User-defined types
This section shows how to define custom types using enum
, union
, struct
, and class
. The latter two will be the focus for most of the remainder of the chapter.
Enumerations
The anonymous enumeration in D declares a set of immutable values. A major difference from C is that it's possible to specify the underlying type. When no fields are explicitly assigned a value, the underlying type of an enum
defaults to int
. Note that user-defined type declarations in D do not require a semicolon at the end:
enum {top, bottom, left, right} // type is int enum : ubyte {red, green, blue, alpha} // type is ubyte
The members of each will be initialized with sequential values starting at 0
. In the second declaration, the underlying type is explicitly set to ubyte
by appending a colon and the type name to the enum
keyword. enum values aren't restricted to integrals, or even just the basic types. Any type that D supports, be it one of the derived data types or even a user-defined type...