Anonymous enums specification

Nick Sabalausky a at a.a
Thu Jun 2 21:52:21 PDT 2011


"Lennart Blanco" <cokebuttle at gmail.com> wrote in message 
news:mailman.560.1307076213.14074.digitalmars-d at puremagic.com...
> Hi
>
> The page for enums specification
> http://www.digitalmars.com/d/2.0/enum.htmldefines enum body syntax as
> follows:
>
> EnumBody:
> ;
> { EnumMembers }
>
> Should it not be
>
> EnumBody:
> EnumMember ;
> { EnumMembers }
>
> or perhaps
>
> EnumBody:
> EnumMembers ;
> { EnumMembers }
>
> Otherwise, I can't quite grasp how following enums definitions are legal:
>
> enum X = 4;
>
> enum
>  mega = 1024 * 1024,
>  pi = 3.14,
>  euler = 2.72,
>  greet = "Hello";
>
> (Both of the above enums are accepted by dmd v2.050).
>

It's a poorly-named hack to allow people to create manifest constants. Ie, 
they're like immutable values, but they don't actually take up any space in 
memory. In other words, it works just like C's "#define SOME_VALUE 5": the 
value just gets substituted into the code wherever the name is found.

But keep in mind that means it's not good to use that enum trick for arrays 
and AAs, because then a whole new array or AA will get allocated everywhere 
the enum is actually used.





More information about the Digitalmars-d mailing list