D programming Language



Download 1.66 Mb.
Page8/47
Date08.01.2017
Size1.66 Mb.
#7507
1   ...   4   5   6   7   8   9   10   11   ...   47

Const Attribute


const

The const attribute declares constants that can be evaluated at compile time. For example:

const int foo = 7;
const

{

double bar = foo + 6;



}


Override Attribute


override

The override attribute applies to virtual functions. It means that the function must override a function with the same name and parameters in a base class. The override attribute is useful for catching errors when a base class's member function gets its parameters changed, and all derived classes need to have their overriding functions updated.

class Foo

{

int bar();



int abc(int x);

}
class Foo2 : Foo

{

override



{

int bar(char c); // error, no bar(char) in Foo

int abc(int x); // ok

}

}




Static Attribute


static

The static attribute applies to functions and data. It means that the declaration does not apply to a particular instance of an object, but to the type of the object. In other words, it means there is no this reference.

class Foo

{

static int bar() { return 6; }



int foobar() { return 7; }

}
...


Foo f;

Foo.bar(); // produces 6

Foo.foobar(); // error, no instance of Foo

f.bar(); // produces 6;

f.foobar(); // produces 7;

Static functions are never virtual.

Static data has only one instance for the entire program, not once per object.

Static does not have the additional C meaning of being local to a file. Use the private attribute in D to achieve that. For example:

module foo;

int x = 3; // x is global

private int y = 4; // y is local to module foo

Static can be applied to constructors and destructors, producing static constructors and static destructors.


Auto Attribute


auto

The auto attribute is used for local variables and for class declarations. For class declarations, the auto attribute creates an auto class. For local declarations, auto implements the RAII (Resource Acquisition Is Initialization) protocol. This means that the destructor for an object is automatically called when the auto reference to it goes out of scope. The destructor is called even if the scope is exited via a thrown exception, thus auto is used to guarantee cleanup.

Auto cannot be applied to globals, statics, data members, inout or out parameters. Arrays of autos are not allowed, and auto function return values are not allowed. Assignment to an auto, other than initialization, is not allowed. Rationale: These restrictions may get relaxed in the future if a compelling reason to appears.

Expressions


C and C++ programmers will find the D expressions very familiar, with a few interesting additions.

Expressions are used to compute values with a resulting type. These values can then be assigned, tested, or ignored. Expressions can also have side effects.

Expression:

AssignExpression

AssignExpression , Expression
AssignExpression:

ConditionalExpression

ConditionalExpression = AssignExpression

ConditionalExpression += AssignExpression

ConditionalExpression -= AssignExpression

ConditionalExpression *= AssignExpression

ConditionalExpression /= AssignExpression

ConditionalExpression %= AssignExpression

ConditionalExpression &= AssignExpression

ConditionalExpression |= AssignExpression

ConditionalExpression ^= AssignExpression

ConditionalExpression ~= AssignExpression

ConditionalExpression <<= AssignExpression

ConditionalExpression >>= AssignExpression

ConditionalExpression >>>= AssignExpression
ConditionalExpression:

OrOrExpression

OrOrExpression ? Expression : ConditionalExpression
OrOrExpression:

AndAndExpression

AndAndExpression || AndAndExpression
AndAndExpression:

OrExpression

OrExpression && OrExpression
OrExpression:

XorExpression

XorExpression | XorExpression
XorExpression:

AndExpression

AndExpression ^ AndExpression
AndExpression:

EqualExpression

EqualExpression & EqualExpression
EqualExpression:

RelExpression

RelExpression == RelExpression

RelExpression != RelExpression

RelExpression === RelExpression

RelExpression !== RelExpression


RelExpression:

ShiftExpression

ShiftExpression < ShiftExpression

ShiftExpression <= ShiftExpression

ShiftExpression > ShiftExpression

ShiftExpression >= ShiftExpression

ShiftExpression !<>= ShiftExpression

ShiftExpression !<> ShiftExpression

ShiftExpression <> ShiftExpression

ShiftExpression <>= ShiftExpression

ShiftExpression !> ShiftExpression

ShiftExpression !>= ShiftExpression

ShiftExpression !< ShiftExpression

ShiftExpression !<= ShiftExpression

ShiftExpression in ShiftExpression
ShiftExpression:

AddExpression

AddExpression << AddExpression

AddExpression >> AddExpression

AddExpression >>> AddExpression
AddExpression:

MulExpression

MulExpression + MulExpression

MulExpression - MulExpression

MulExpression ~ MulExpression
MulExpression:

UnaryExpression

UnaryExpression * UnaryExpression

UnaryExpression / UnaryExpression

UnaryExpression % UnaryExpression
UnaryExpression:

PostfixExpression

& UnaryExpression

++ UnaryExpression

-- UnaryExpression

* UnaryExpression

- UnaryExpression

+ UnaryExpression

! UnaryExpression

~ UnaryExpression

delete UnaryExpression



NewExpression

( Type ) UnaryExpression

( Type ) . Identifier
PostfixExpression:

PrimaryExpression

PostfixExpression . Identifier

PostfixExpression ++

PostfixExpression --

PostfixExpression ( ArgumentList )

PostfixExpression [ Expression ]
PrimaryExpression:

Identifier

this

super

null

true

false



NumericLiteral

StringLiteral

FunctionLiteral

AssertExpression

Type . Identifier
AssertExpression:

assert ( Expression )


ArgumentList:

AssignExpression

AssignExpression , ArgumentList
NewExpression:

new BasicType Stars [ AssignExpression ] Declarator

new BasicType Stars ( ArgumentList )

new BasicType Stars

new ( ArgumentList ) BasicType Stars [ AssignExpression ] Declarator

new ( ArgumentList ) BasicType Stars ( ArgumentList )

new ( ArgumentList ) BasicType Stars
Stars

nothing

*

* Stars


Download 1.66 Mb.

Share with your friends:
1   ...   4   5   6   7   8   9   10   11   ...   47




The database is protected by copyright ©ininet.org 2024
send message

    Main page