www.digitalmars.com [Home] [Search] [D]

Last update Feb 4, 2002


Types

Basic Data Types

	void		no type
	bit		single bit
	byte		signed 8 bits
	ubyte		unsigned 8 bits
	short		signed 16 bits
	ushort		unsigned 16 bits
	int		signed 32 bits
	uint		unsigned 32 bits
	long		signed 64 bits
	ulong		unsigned 64 bits
	float		32 bit floating point
	double		64 bit floating point
	extended	largest hardware implemented floating
			point size (Implementation Note: 80 bits for Intel CPU's)
	imaginary	an extended floating point value, but with imaginary
			type
	complex		two extended floating point values, one real and the other
			imaginary

	char		unsigned 8 bit ASCII
	wchar		unsigned Wide char (Implementation Note: 16 bits on Win32
			systems, 32 bits on linux, corresponding to C's wchar_t
			type)
	
The bit data type is special. It means one binary bit. Pointers or references to a bit are not allowed.

Derived Data Types

User Defined Types

Pointer Conversions

Casting pointers to non-pointers and vice versa is not allowed in D. This is to prevent casual manipulation of pointers as integers, as these kinds of practices can play havoc with the garbage collector and in porting code from one machine to another. If it is really, absolutely, positively necessary to do this, use a union, and even then, be very careful that the garbage collector won't get botched by this.

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 = (myint)i;	// OK
	

Integer Promotions

The following types are implicitly converted to int:
	bit
	byte
	ubyte
	short
	ushort
	enum
    
Typedefs are converted to their underlying type.

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 extended, the other operand is converted to extended.
  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.

Copyright (c) 1999-2002 by Digital Mars, All Rights Reserved