bool
Mixin Types
size_t
ptrdiff_t
D is statically typed. Every expression has a type. Types constrain the values an expression can hold, and determine the semantics of operations on those values.
Basic data types are leaf types. Derived data types build on leaf types. User defined types are aggregates of basic and derived types.
Keyword | Default Initializer (.init ) |
Description |
---|---|---|
void |
- |
no type |
bool |
false |
boolean value |
byte |
0 |
signed 8 bits |
ubyte |
0u |
unsigned 8 bits |
short |
0 |
signed 16 bits |
ushort |
0u |
unsigned 16 bits |
int |
0 |
signed 32 bits |
uint |
0u |
unsigned 32 bits |
long |
0L |
signed 64 bits |
ulong |
0uL |
unsigned 64 bits |
cent |
0 |
signed 128 bits (reserved for future use) |
ucent |
0u |
unsigned 128 bits (reserved for future use) |
float |
float.nan |
32 bit floating point |
double |
double.nan |
64 bit floating point |
real |
real.nan |
largest FP size implemented in hardwareImplementation Note: 80 bits for x86 CPUs or double size, whichever is larger
|
ifloat |
float.nan*1.0i |
imaginary float |
idouble |
double.nan*1.0i |
imaginary double |
ireal |
real.nan*1.0i |
imaginary real |
cfloat |
float.nan+float.nan*1.0i |
a complex number of two float values |
cdouble |
double.nan+double.nan*1.0i |
complex double |
creal |
real.nan+real.nan*1.0i |
complex real |
char |
'xFF' |
unsigned 8 bit (UTF-8 code unit) |
wchar |
'uFFFF' |
unsigned 16 bit (UTF-16 code unit) |
dchar |
'U0000FFFF' |
unsigned 32 bit (UTF-32 code unit) |
Strings are a special case of arrays.
The base type of an enum is the type it is based on:
enum E : T { ... } // T is the base type of E
Casting pointers to non-pointers and vice versa is allowed.
Best Practices: do not do this for any pointers that point to data allocated by the garbage collector.Implicit conversions are used to automatically convert types as required.
An 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 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 an enum has as a base type one of the types in the left column, it is converted to the type in the right column.
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:
real
, the other operand is converted to real
.double
, the other operand is converted to double
.float
, the other operand is converted to float
.If one or both of the operand types is an enum after undergoing the above conversions, the result type is:
Integer values cannot be implicitly converted to another type that cannot represent the integer bit pattern after integral promotion. For example:
ubyte u1 = -1; // error, -1 cannot be represented in a ubyte ushort u2 = -1; // error, -1 cannot be represented in a ushort uint u3 = int(-1); // ok, -1 can be represented in a uint ulong u4 = long(-1); // ok, -1 can be represented in a ulong
Floating point types cannot be implicitly converted to integral types. Complex or imaginary floating point types cannot be implicitly converted to non-complex floating point types. Non-complex floating point types cannot be implicitly converted to imaginary floating point types.
bool
The bool type is a 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 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:
int function(int) fp; // fp is pointer to a function int delegate(int) dg; // dg is a delegate 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)
The C style syntax for declaring pointers to functions is deprecated:
int (*fp)(int); // fp is pointer to a function
Mixin Types
MixinType: mixin ( ArgumentList )
Each AssignExpression in the ArgumentList is evaluated at compile time, and the result must be representable as a string. The resulting strings are concatenated to form a string. The text contents of the string must be compilable as a valid Type, and is compiled as such.
void test(mixin("int")* p) // int* p { mixin("int")[] a; // int[] a; mixin("int[]") b; // int[] b; }
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 addressable memory.
ptrdiff_t
ptrdiff_t
is an alias to the signed integral basic type the same size as size_t
.
© 1999–2019 The D Language Foundation
Licensed under the Boost License 1.0.
https://dlang.org/spec/type.html