D Programming/Types

From Wikibooks, open books for an open world
Jump to: navigation, search

To be completed.

Clipboard

To do:

D Data Types[edit]

If you look directly at the content of a computer's memory, you see only a lot of bytes. These are often shown as numbers but in fact they don't have a meaning on its own, not even the meaning of a number.

Types are used in computer languages like D to give one byte or a sequence of bytes a meaning. Types define if these bytes are a number, a text, a date or whatever.


Basic Types[edit]

Void Type[edit]

  • void

Boolean Type[edit]

  • bool

Integer Types[edit]

Provided types are:

  • byte
  • ubyte
  • short
  • ushort
  • int
  • uint
  • long
  • ulong
  • cent (reserved for future use)
  • ucent (reserved for future use)


Each type has a size in bits which defines how many different values(=states) it can hold.

An 8 bit type, for instance, can store 256 different states, while a 16 bit type can store 65536.


The sizes of the types are:

Name Size in Bits Size in Octets Min Max Comment
byte 8 1 -128 +127
short 16 2 -32768 +32767
int 32 4 -2147483648 +2147483647
long 64 8 -9223372036854775808 +9223372036854775807
cent 128 16 -170141183460469231731687303715884105728 +170141183460469231731687303715884105727


Each type exists as signed and unsigned variant. The unsigned ones have an "u" prefix (like uint, ubyte).

Unsigned means that the range of possible states is interpreted as nonnegative values starting at 0 and going up to the maximum number of possible states for this type minus 1 because the 0 is also a state which must be stored.

For the ubyte type (8 bit = 256 states) this means it can store values from 0 up to and including 255.

Signed types interpret one half of their states as positive, the other half as negative values, so the signed byte type has a range of values from -128 to +127.

Floating-Point Types[edit]

  • float
  • double
  • real
  • ifloat
  • idouble
  • ireal
  • cfloat
  • cdouble
  • creal

Character Types[edit]

A character is a unit of information that roughly corresponds to a grapheme-like unit, or symbol, such as in an alphabet in the written form of a natural language. The concept also includes control characters, such as carriage return.

Characters are typically combined into strings.

D language allows to directly handle Unicode strings.

  • char (to handle UTF8 code units)
  • wchar (to handle UTF16 code units)
  • dchar (to handle UTF32 code units)

Derived Types[edit]

  • pointer
  • array
  • associative array
  • function
  • delegate

User Defined Types[edit]

  • alias
  • typedef
  • enum
  • struct
  • union
  • class