· Lexical · Modules · Declarations · Types · Properties · Attributes · Pragmas · Expressions · Statements · Arrays · Structs & Unions · Classes · Interfaces · Enums · Functions · Operator Overloading · Templates · Mixins · Contracts · Conditional Compilation · Handling errors · Garbage Collection · Memory Management · Floating Point · Inline Assembler · Documentation Comments · Interfacing To C · Portability Guide · Embedding D in HTML · Named Character Entities · Application Binary Interface |
TypesBasic Data Types
Derived Data Types
User Defined Types
Pointer ConversionsCasting pointers to non-pointers and vice versa is allowed in D, however, do not do this for any pointers that point to data allocated by the garbage collector.Implicit ConversionsD has a lot of types, both built in and derived. It would be tedious to require casts for every type conversion, so implicit conversions step in to handle the obvious ones automatically.A typedef can be implicitly converted to its underlying type, but going the other way requires an explicit conversion. For example: typedef int myint; int i; myint m; i = m; // OK m = i; // error m = cast(myint)i; // OK Integer PromotionsInteger Promotions are conversions of the following types:
If a typedef or enum has as a base type one of the types in the left column, it is converted to the type in the right column. Usual Arithmetic ConversionsThe usual arithmetic conversions convert operands of binary operators to a common type. The operands must already be of arithmetic types. The following rules are applied in order:
Complex floating point types cannot be implicitly converted to non-complex floating point types. Imaginary floating point types cannot be implicitly converted to float, double, or real types. Float, double, or real types cannot be implicitly converted to imaginary floating point types. boolThe bool type is a 1 byte size type that can only hold the value true or false. The only operators that can accept operands of type bool are: & | ^ &= |= ^= ! && || ?:. A bool value can be implicitly converted to any integral type, with false becoming 0 and true becoming 1. The numeric literals 0 and 1 can be implicitly converted to the bool values false and true, respectively. Casting an expression to bool means testing for 0 or !=0 for arithmetic types, and null or !=null for pointers or references.DelegatesThere are no pointers-to-members in D, but a more useful concept called delegates are supported. Delegates are an aggregate of two pieces of data: an object reference and a function pointer. The object reference forms the this pointer when the function is called.Delegates are declared similarly to function pointers, except that the keyword delegate takes the place of (*), and the identifier occurs afterwards: int function(int) fp; // fp is pointer to a function int delegate(int) dg; // dg is a delegate to a functionThe C style syntax for declaring pointers to functions is also supported: int (*fp)(int); // fp is pointer to a functionA delegate is initialized analogously to function pointers: int func(int); fp = &func; // fp points to func class OB { int member(int); } OB o; dg = &o.member; // dg is a delegate to object o and // member function memberDelegates cannot be initialized with static member functions or non-member functions. Delegates are called analogously to function pointers: fp(3); // call func(3) dg(3); // call o.member(3) |