www.digitalmars.com [Home] [Search] [D]
Last update Nov 29, 2004

Types

Basic Data Types

Derived Data Types

User Defined Types

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
bit 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:
  1. Typedefs are converted to their underlying type.
  2. If either operand is real, the other operand is converted to real.
  3. Else if either operand is double, the other operand is converted to double.
  4. Else if either operand is float, the other operand is converted to float.
  5. 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.

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 function
	
The C style syntax for declaring pointers to functions is also supported:
	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)
	

Feedback and Comments

Add feedback and comments regarding this page.
Copyright (c) 1999-2004 by Digital Mars, All Rights Reserved