Types
Basic Data Types
Keyword | Description | Default Initializer (.init) | ||||||
---|---|---|---|---|---|---|---|---|
void | no type | - | ||||||
bool | boolean value | false | ||||||
byte | signed 8 bits | 0 | ||||||
ubyte | unsigned 8 bits | 0 | ||||||
short | signed 16 bits | 0 | ||||||
ushort | unsigned 16 bits | 0 | ||||||
int | signed 32 bits | 0 | ||||||
uint | unsigned 32 bits | 0 | ||||||
long | signed 64 bits | 0L | ||||||
ulong | unsigned 64 bits | 0L | ||||||
cent | signed 128 bits (reserved for future use) | 0 | ||||||
ucent | unsigned 128 bits (reserved for future use) | 0 | ||||||
float | 32 bit floating point | float.nan | ||||||
double | 64 bit floating point | double.nan | ||||||
real | largest hardware implemented floating point size (Implementation Note: 80 bits for Intel CPU's) | real.nan | ||||||
ifloat | imaginary float | float.nan * 1.0i | ||||||
idouble | imaginary double | double.nan * 1.0i | ||||||
ireal | imaginary real | real.nan * 1.0i | ||||||
cfloat | a complex number of two float values | float.nan + float.nan * 1.0i | ||||||
cdouble | complex double | double.nan + double.nan * 1.0i | ||||||
creal | complex real | real.nan + real.nan * 1.0i | ||||||
char unsigned 8 bit UTF-8 | 0xFF
| wchar | unsigned 16 bit UTF-16 | 0xFFFF
| dchar | unsigned 32 bit UTF-32 | 0x0000FFFF
| |
Derived Data Types
- pointer
- array
- associative array
- function
- delegate
User Defined Types
- alias
- typedef
- enum
- struct
- union
- class
Pointer Conversions
Casting 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 Conversions
D 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 Promotions
Integer Promotions are conversions of the following types:
from | to |
---|---|
bool | int |
byte | int |
ubyte | int |
short | int |
ushort | int |
char | int |
wchar | int |
dchar | uint |
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 Conversions
The 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:- Typedefs are converted to their underlying type.
- If either operand is real, the other operand is converted to real.
- Else if either operand is double, the other operand is converted to double.
- Else if either operand is float, the other operand is converted to float.
- Else the integer promotions are done on each operand,
followed by:
- If both are the same type, no more conversions are done.
- If both are signed or both are unsigned, the smaller type is converted to the larger.
- If the signed type is larger than the unsigned type, the unsigned type is converted to the signed type.
- The signed type is converted to the unsigned type.
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.
bool
The 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.Delegates
There 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)