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; m = i; m = cast(myint)i;
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, looking at the base type (looking past typedefs):
- 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.
If one or both of the operand types is a typedef after
undergoing the above conversions, the result type is:
- If the operands are the same type, the result will be the
that type.
- If one operand is a typedef and the other is the base type
of that typedef, the result is the base type.
- If the two operands are different typedefs but of the same
base type, then the result is that base 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; ushort u2 = cast(short)-1; uint u3 = cast(int)-1; ulong u4 = cast(ulong)-1;
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.
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; int delegate(int) dg;
The C style syntax for declaring pointers to functions is
also supported:
int (*fp)(int);
A delegate is initialized analogously to function pointers:
int func(int);
fp = &func;
class OB
{ int member(int);
}
OB o;
dg = &o.member;
Delegates cannot be initialized with static member functions
or non-member functions.
Delegates are called analogously to function pointers:
fp(3); dg(3);
Copyright © 1999-2006 by Digital Mars, All Rights Reserved
|
Page generated by Ddoc. |
Comments
|