Improve this page Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone. Page wiki View or edit the community-maintained wiki page associated with this page.

Types

Basic Data Types

Basic Data Types
KeywordDefault Initializer (.init)Description
void-no type
boolfalseboolean value
byte0signed 8 bits
ubyte0unsigned 8 bits
short0signed 16 bits
ushort0unsigned 16 bits
int0signed 32 bits
uint0unsigned 32 bits
long0Lsigned 64 bits
ulong0Lunsigned 64 bits
cent0signed 128 bits (reserved for future use)
ucent0unsigned 128 bits (reserved for future use)
floatfloat.nan32 bit floating point
doubledouble.nan64 bit floating point
realreal.nanlargest FP size implemented in hardwareImplementation Note: 80 bits for x86 CPUs or double size, whichever is larger
ifloatfloat.nan*1.0iimaginary float
idoubledouble.nan*1.0iimaginary double
irealreal.nan*1.0iimaginary real
cfloatfloat.nan+float.nan*1.0ia complex number of two float values
cdoubledouble.nan+double.nan*1.0icomplex double
crealreal.nan+real.nan*1.0icomplex real
char0xFFunsigned 8 bit (UTF-8 code unit)
wchar0xFFFFunsigned 16 bit (UTF-16 code unit)
dchar0x0000FFFFunsigned 32 bit (UTF-32 code unit)

Derived Data Types

Strings are a special case of arrays.

User Defined Types

Base Types

The base type of an enum is the type it is based on:

enum E : T { ... }  // T is the base type of E

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

Implicit conversions are used to automatically convert types as required.

A enum can be implicitly converted to its base type, but going the other way requires an explicit conversion. For example:

int i;

enum Foo { E }
Foo f;
i = f;           // OK
f = i;           // error
f = cast(Foo)i;  // OK
f = 0;           // error
f = Foo.E;       // OK

Integer Promotions

Integer Promotions are conversions of the following types:

Integer Promotions
fromto
bool int
byte int
ubyte int
short int
ushort int
char int
wchar int
dchar uint

If a 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, looking at the base type:

  1. If either operand is real, the other operand is converted to real.
  2. Else if either operand is double, the other operand is converted to double.
  3. Else if either operand is float, the other operand is converted to float.
  4. Else the integer promotions are done on each operand, followed by:
    1. If both are the same type, no more conversions are done.
    2. If both are signed or both are unsigned, the smaller type is converted to the larger.
    3. If the signed type is larger than the unsigned type, the unsigned type is converted to the signed type.
    4. The signed type is converted to the unsigned type.

If one or both of the operand types is an enum after undergoing the above conversions, the result type is:

  1. If the operands are the same type, the result will be the that type.
  2. If one operand is an enum and the other is the base type of that enum, the result is the base type.
  3. If the two operands are different enums, the result is the closest base type common to both. A base type being closer means there is a shorter sequence of conversions to base type to get there from the original type.

Integer values cannot be implicitly converted to another type that cannot represent the integer bit pattern after integral promotion. For example:

ubyte  u1 = cast(byte)-1;  // error, -1 cannot be represented in a ubyte
ushort u2 = cast(short)-1; // error, -1 cannot be represented in a ushort
uint   u3 = cast(int)-1;   // ok, -1 can be represented in a uint
ulong  u4 = cast(long)-1; // ok, -1 can be represented in a ulong

Floating point types cannot be implicitly converted to integral types.

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 pointer to a non-static member function, or a pointer to a closure and a pointer to a nested function. 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 function

The C style syntax for declaring pointers to functions is deprecated:

int (*fp)(int);  // fp is pointer to a function

A 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 member

Delegates 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)

The equivalent of member function pointers can be constructed using anonymous lambda functions:

class C
{
    int a;
    int foo(int i) { return i + a; }
}

// mfp is the member function pointer
auto mfp = function(C self, int i) { return self.foo(i); };
auto c = new C();  // create an instance of C
mfp(c, 1);  // and call c.foo(1)

size_t

size_t is an alias to one of the unsigned integral basic types, and represents a type that is large enough to represent an offset into all addressible memory.

ptrdiff_t

ptrdiff_t is an alias to the signed basic type the same size as size_t.