www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Anonymous enums specification

reply Lennart Blanco <cokebuttle gmail.com> writes:
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).

Regards,
Lennart
Jun 02 2011
parent reply "Nick Sabalausky" <a a.a> writes:
"Lennart Blanco" <cokebuttle gmail.com> wrote in message 
news:mailman.560.1307076213.14074.digitalmars-d 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.
Jun 02 2011
parent Lennart Blanco <cokebuttle gmail.com> writes:
On Fri, Jun 3, 2011 at 6:52 AM, Nick Sabalausky <a a.a> wrote:

 "Lennart Blanco" <cokebuttle gmail.com> wrote in message
 news:mailman.560.1307076213.14074.digitalmars-d puremagic.com...
 Hi

 The page for enums specification
 http://www.digitalmars.com/d/2.0/enum.html
defines<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.
Yes, I understand how anonymous enums works. My question was regarding the D 2.0 specification document. To me it looks like the syntax for anonymous enums is missing in the specification. Only valid enum body definitions are empty body or a list of enum members, delimited with '{' abd '}'. /Lennart
Jun 04 2011