D Programming Language 1.0

Last update Sun Dec 30 20:34:42 2012

# Enums

    EnumDeclaration:
        enum EnumTag EnumBody
        enum EnumBody
        enum EnumTag : EnumBaseType EnumBody
        enum : EnumBaseType EnumBody

    EnumTag:
        Identifier

    EnumBaseType:
        Type

    EnumBody:
        EmptyEnumBody
        EnumMembersBody

    EmptyEnumBody:
        ;

    EnumMembersBody:
        { EnumMembers }

    EnumMembers:
        EnumMember
        EnumMember ,
        EnumMember , EnumMembers

    EnumMember:
        Identifier
        Identifier = AssignExpression

Enum declarations are used to define a group of constants. They come in two forms:

1. Named enums, which have an EnumTag.
2. Anonymous enums, which do not have an EnumTag.

## Named Enums

Named enums are used to declare related constants and group them by giving them a unique type. The EnumMembers are declared in the scope of the enum EnumTag. The enum EnumTag declares a new type, and all the EnumMembers have that type.

This defines a new type `X` which has values `X.A=0`, `X.B=1`, `X.C=2`:

    enum X { A, B, C }	// named enum

If the EnumBaseType is not explicitly set, it is set to type `int`.

A named enum member can be implicitly cast to its EnumBaseType, but EnumBaseType types cannot be implicitly cast to an enum type.

The value of an EnumMember is given by its initializer. If there is no initializer, it is given the value of the previous EnumMember + 1. If it is the first EnumMember, it's value is 0.

An EmptyEnumBody signifies an opaque enum - the enum members are unknown.

### Enum Default Initializer

The `.init` property of an enum type is the value of the first member of that enum. This is also the default initializer for the enum type.

    enum X { A=3, B, C }
    X x;      // x is initialized to 3

### Enum Properties

Enum properties only exist for named enums.

.init | First enum member value
.min | Smallest value of enum
.max | Largest value of enum
.sizeof | Size of storage for an enumerated value

For example:

    enum X { A=3, B, C }
    X.min    // is X.A
    X.max    // is X.C
    X.sizeof // is same as int.sizeof

## Anonymous Enums

If the enum Identifier is not present, then the enum is an anonymous enum, and the EnumMembers are declared in the scope the EnumDeclaration appears in. No new type is created; the EnumMembers have the type of the EnumBaseType.

The EnumBaseType is the underlying type of the enum.

It must be an integral type. If omitted, it defaults to int.

    enum { A, B, C }	// anonymous enum

Defines the constants A=0, B=1, C=2, all of type int.

Enums must have at least one member.

The value of an EnumMember is given by its initializer. If there is no initializer, it is given the value of the previous EnumMember + 1. If it is the first EnumMember, it's value is 0.

    enum { A, B = 5+7, C, D = 8+C, E }

Sets A=0, B=12, C=13, D=21, and E=22, all of type int.

    enum : long { A = 3, B }

Sets A=3, B=4 all of type long.

Forums | Comments | D | Search | Downloads | Home
Copyright © 1999-2012 by Digital Mars ®, All Rights Reserved | Page generated by Ddoc.