D Programming/Types

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

To be completed.


Clipboard

To do:


D Data Types[edit | edit source]

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 | edit source]

Void Type[edit | edit source]

  • void

Boolean Type[edit | edit source]

  • bool

Integer Types[edit | edit source]

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
byte 8 1 -128 +127
ubyte 8 1 0 +255
short 16 2 -32,768 +32,767
ushort 16 2 0 +65,535
int 32 4 -2,147,483,648 +2,147,483,647
uint 32 4 0 +4,294,967,295
long 64 8 -9,223,372,036,854,775,808 +9,223,372,036,854,775,807
ulong 64 8 0 +18,446,744,073,709,551,615
cent 128 16 -170,141,183,460,469,231,731,687,303,715,884,105,728 +170,141,183,460,469,231,731,687,303,715,884,105,727

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 | edit source]

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

Character Types[edit | edit source]

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 | edit source]

  • pointer
  • array
  • associative array
  • function
  • delegate

User Defined Types[edit | edit source]

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