$(DDOC $(DDOC_BLANKLINE ) $(DDOC_BLANKLINE ) $(SPEC_S Enums, $(DDOC_BLANKLINE ) $(HEADERNAV_TOC $(HEADERNAV_SUBITEMS named_enums, Named Enums, $(HEADERNAV_ITEM enum_variables, Enum Variables) $(HEADERNAV_ITEM enum_properties, Enum Properties) $(HEADERNAV_ITEM enum_copying_and_assignment, Enum Copying and Assignment) ) $(HEADERNAV_SUBITEMS anonymous_enums, Anonymous Enums, $(HEADERNAV_ITEM single_member, Single Member Syntax) ) $(HEADERNAV_ITEM manifest_constants, Manifest Constants) ) $(DDOC_BLANKLINE ) $(GRAMMAR $(GNAME EnumDeclaration): $(D enum) $(GLINK_LEX Identifier) $(GLINK EnumBody) $(D enum) $(GLINK_LEX Identifier) $(D :) $(GLINK EnumBaseType) $(GLINK EnumBody) $(GLINK AnonymousEnumDeclaration) $(DDOC_BLANKLINE ) $(GNAME EnumBaseType): $(GLINK2 type, Type) $(DDOC_BLANKLINE ) $(GNAME EnumBody): $(D {) $(GLINK EnumMembers) $(D }) $(D ;) $(DDOC_BLANKLINE ) $(GNAME EnumMembers): $(GLINK EnumMember) $(GLINK EnumMember) $(D ,) $(GLINK EnumMember) $(D ,) $(GSELF EnumMembers) $(DDOC_BLANKLINE ) $(GNAME EnumMember): $(GLINK EnumMemberAttributes)$(OPT ) $(GLINK_LEX Identifier) $(GLINK EnumMemberAttributes)$(OPT ) $(GLINK_LEX Identifier) $(D =) $(ASSIGNEXPRESSION ) $(DDOC_BLANKLINE ) $(GNAME EnumMemberAttributes): $(GLINK EnumMemberAttribute) $(GLINK EnumMemberAttribute) $(GSELF EnumMemberAttributes) $(DDOC_BLANKLINE ) $(GNAME EnumMemberAttribute): $(GLINK2 attribute, DeprecatedAttribute) $(GLINK2 attribute, UserDefinedAttribute) $(D @)$(LINK2 attribute.html#disable, $(D disable)) ) $(GRAMMAR $(GNAME AnonymousEnumDeclaration): $(D enum) $(D :) $(GLINK EnumBaseType) $(D {) $(GLINK EnumMembers) $(D }) $(D enum) $(D {) $(GLINK AnonymousEnumMembers) $(D }) $(DDOC_BLANKLINE ) $(GNAME AnonymousEnumMembers): $(GLINK AnonymousEnumMember) $(GLINK AnonymousEnumMember) $(D ,) $(GLINK AnonymousEnumMember) $(D ,) $(GSELF AnonymousEnumMembers) $(DDOC_BLANKLINE ) $(GNAME AnonymousEnumMember): $(GLINK EnumMember) $(GLINK EnumMemberAttributes)$(OPT ) $(GLINK2 type, Type) $(GLINK_LEX Identifier) $(D =) $(ASSIGNEXPRESSION ) ) $(DDOC_BLANKLINE ) $(P Enum declarations are used to define a group of constants. ) $(DDOC_BLANKLINE )
0
.
If there is no AssignExpression and it is not the first $(I EnumMember),
it is given the value of the previous $(I EnumMember)+1
:)
$(UL $(LI If the value of the previous $(I EnumMember) is $(GLINK EnumBaseType).max
,
it is an error. This prevents value overflow. It is an error if the previous
member cannot be compared with EnumBaseType.max
at compile-time.)
$(LI It is an error if the base type does not define a compile-time
evaluable +1
operation.)
$(LI If the value of the previous $(I EnumMember)+1
is the same as the
value of the previous $(I EnumMember), it is an error. (This can happen
with floating point types).
)
)
$(SPEC_RUNNABLE_EXAMPLE_FAIL $(D_CODE $(D_KEYWORD enum) E : $(D_KEYWORD char)
{
a,
b = $(D_KEYWORD char).max,
c $(D_COMMENT // overflow
)}
$(D_KEYWORD static) $(D_KEYWORD assert)(E.a == 0);
)
)
$(DDOC_BLANKLINE )
$(P All $(I EnumMember)s are in scope for the AssignExpressions.
)
$(DDOC_BLANKLINE )
$(SPEC_RUNNABLE_EXAMPLE_FAIL $(D_CODE $(D_KEYWORD enum) A = 3;
$(D_KEYWORD enum) B
{
A = A $(D_COMMENT // error, circular reference
)}
$(D_KEYWORD enum) C
{
A = B, $(D_COMMENT // A = 4
) B = D, $(D_COMMENT // B = 4
) C = 3, $(D_COMMENT // C = 3
) D $(D_COMMENT // D = 4
)}
$(D_KEYWORD enum) E : C
{
E1 = C.D,
E2 $(D_COMMENT // error, C.D is C.max
)}
)
)
$(DDOC_BLANKLINE )
$(P An empty enum body signifies an opaque enum - the enum members are unknown.)
$(D_CODE $(D_KEYWORD enum) X; $(D_COMMENT // opaque enum
)writeln(X.init); $(D_COMMENT // error: enum X is opaque and has no default initializer
))
$(DDOC_BLANKLINE )
final switch
).)
$(DDOC_BLANKLINE )
struct
with
a copy constructor, the copy constructor is not called:)
$(DDOC_BLANKLINE )
$(SPEC_RUNNABLE_EXAMPLE_RUN $(D_CODE $(D_KEYWORD struct) S
{
$(D_KEYWORD this)($(D_KEYWORD ref) S rhs) { $(D_KEYWORD assert)(0); }
}
$(D_KEYWORD enum) E : S { A = S.init }
$(D_KEYWORD void) main()
{
E e1;
E e2 = e1; $(D_COMMENT // ok - copy constructor not called
)}
)
)
$(DDOC_BLANKLINE )
$(P When copying a named enum value whose base type is a struct
with
a postblit, the postblit is not called:)
$(DDOC_BLANKLINE )
$(SPEC_RUNNABLE_EXAMPLE_RUN $(D_CODE $(D_KEYWORD struct) S
{
$(D_KEYWORD this)($(D_KEYWORD this)) { $(D_KEYWORD assert)(0); }
}
$(D_KEYWORD enum) E : S { A = S.init }
$(D_KEYWORD void) main()
{
E e1;
E e2 = e1; $(D_COMMENT // ok - postblit not called
)}
)
)
$(DDOC_BLANKLINE )
$(P When assigning a named enum value to another object of the same
type, if the base type of those values is a struct
with an identity
assignment overload, the identity assignment overload is not called:)
$(DDOC_BLANKLINE )
$(SPEC_RUNNABLE_EXAMPLE_RUN $(D_CODE $(D_KEYWORD struct) S
{
$(D_KEYWORD void) opAssign(S rhs) { $(D_KEYWORD assert)(0); }
}
$(D_KEYWORD enum) E : S { A = S.init }
$(D_KEYWORD void) main()
{
E e1, e2;
e2 = e1; $(D_COMMENT // ok - opAssign not called
)}
)
)
$(DDOC_BLANKLINE )
$(DDOC_BLANKLINE )
int
.)
$(DDOC_BLANKLINE )
$(P Enums must have at least one member.
)
$(DDOC_BLANKLINE )
$(P The value of an $(I EnumMember) is given by its AssignExpression if present.
If there is no AssignExpression and it is the first $(I EnumMember),
its value is the $(CODE .init) property of the $(I EnumMember)'s type.
If there is no AssignExpression and it is not the first $(I EnumMember),
it is given the value of the previous $(I EnumMember)+1
:)
$(UL $(LI If the value of the previous $(I EnumMember) is the .max
property
of the previous $(I EnumMember)'s type, it is an error.
This prevents value overflow. It is an error if the previous
member cannot be compared with its .max
property at compile-time.)
$(LI It is an error if the type of the previous member does not define a compile-time
evaluable +1
operation.)
$(LI If the value of the previous $(I EnumMember)+1
is the same as the
value of the previous $(I EnumMember), it is an error. (This can happen
with floating point types).
)
)
$(P All $(I EnumMember)s are in scope for the AssignExpressions.
)
$(DDOC_BLANKLINE )
$(D_CODE $(D_KEYWORD enum) { A, B = 5+7, C, D = 8+C, E }
)
$(DDOC_BLANKLINE )
$(P Sets A=0, B=12, C=13, D=21, and E=22, all of type int
.)
$(DDOC_BLANKLINE )
$(D_CODE $(D_KEYWORD enum) : $(D_KEYWORD long) { A = 3, B }
)
$(DDOC_BLANKLINE )
$(P Sets A=3, B=4 all of type long
.)
$(DDOC_BLANKLINE )
$(D_CODE $(D_KEYWORD enum) : string
{
A = $(D_STRING "hello"),
B = $(D_STRING "betty"),
C $(D_COMMENT // error, cannot add 1 to "betty"
)}
)
$(DDOC_BLANKLINE )
$(D_CODE $(D_KEYWORD enum)
{
A = 1.2f, $(D_COMMENT // A is 1.2f of type float
) B, $(D_COMMENT // B is 2.2f of type float
) $(D_KEYWORD int) C = 3, $(D_COMMENT // C is 3 of type int
) D $(D_COMMENT // D is 4 of type int
)}
)
$(DDOC_BLANKLINE )
{ }
can
be omitted. Gramatically speaking, this is an $(GLINK2 declaration, AutoDeclaration).
)
$(DDOC_BLANKLINE )
$(D_CODE $(D_KEYWORD enum) i = 4; $(D_COMMENT // i is 4 of type int
)$(D_KEYWORD enum) $(D_KEYWORD long) l = 3; $(D_COMMENT // l is 3 of type long
))
$(DDOC_BLANKLINE )