www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Anonymous enums specification

reply Lennart Blanco <cokebuttle gmail.com> writes:
--000e0cd52c901de53904a4c65aa4
Content-Type: text/plain; charset=UTF-8

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

--000e0cd52c901de53904a4c65aa4
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div>Hi</div><div><br></div><div>The page for enums specification <a href=
=3D"http://www.digitalmars.com/d/2.0/enum.html">http://www.digitalmars.com/=
d/2.0/enum.html</a> defines enum body syntax as follows:</div><div><br></di=
v>
<div>EnumBody:</div><div><span class=3D"Apple-tab-span" style=3D"white-spac=
e:pre">	</span>;</div><div><span class=3D"Apple-tab-span" style=3D"white-sp=
ace:pre">	</span>{ EnumMembers }</div><div><br></div><div>Should it not be<=
/div>
<div><br></div><div>EnumBody:</div><div><span class=3D"Apple-tab-span" styl=
e=3D"white-space:pre">	</span>EnumMember ;</div><div><span class=3D"Apple-t=
ab-span" style=3D"white-space:pre">	</span>{ EnumMembers }</div><div><br></=
div>
<div>or perhaps</div><div><br></div><div>EnumBody:</div><div><span class=3D=
"Apple-tab-span" style=3D"white-space:pre">	</span>EnumMembers ;</div><div>=
<span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>{ EnumMemb=
ers }</div>
<div><br></div><div>Otherwise, I can&#39;t quite grasp how following enums =
definitions are legal:</div><div><br></div><div>enum X =3D 4;</div><div><br=
</div><div>enum</div><div>=C2=A0 mega =3D 1024 * 1024,</div><div>=C2=A0 pi=

<div>=C2=A0 euler =3D 2.72,</div><div>=C2=A0 greet =3D &quot;Hello&quot;;</= div><div><br></div><div>(Both of the above enums are accepted by dmd v2.050= ).</div><div><br></div><div>Regards,</div><div>Lennart</div> --000e0cd52c901de53904a4c65aa4--
Jun 02 2011
next sibling parent "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
prev sibling parent Lennart Blanco <cokebuttle gmail.com> writes:
--000e0cd58e681733f504a4e9b2e0
Content-Type: text/plain; charset=UTF-8

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 --000e0cd58e681733f504a4e9b2e0 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <br><br><div class=3D"gmail_quote">On Fri, Jun 3, 2011 at 6:52 AM, Nick Sab= alausky <span dir=3D"ltr">&lt;a a.a&gt;</span> wrote:<br><blockquote class= =3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padd= ing-left:1ex;"> &quot;Lennart Blanco&quot; &lt;<a href=3D"mailto:cokebuttle gmail.com">coke= buttle gmail.com</a>&gt; wrote in message<br> news:mailman.560.1307076213.14074.digitalmars-d puremagic.com...<br> <div class=3D"im">&gt; Hi<br> &gt;<br> &gt; The page for enums specification<br> </div>&gt; <a href=3D"http://www.digitalmars.com/d/2.0/enum.htmldefines" ta= rget=3D"_blank">http://www.digitalmars.com/d/2.0/enum.html defines</a> enum= body syntax as<br> <div class=3D"im">&gt; follows:<br> &gt;<br> &gt; EnumBody:<br> &gt; ;<br> &gt; { EnumMembers }<br> &gt;<br> &gt; Should it not be<br> &gt;<br> &gt; EnumBody:<br> &gt; EnumMember ;<br> &gt; { EnumMembers }<br> &gt;<br> &gt; or perhaps<br> &gt;<br> &gt; EnumBody:<br> &gt; EnumMembers ;<br> &gt; { EnumMembers }<br> &gt;<br> &gt; Otherwise, I can&#39;t quite grasp how following enums definitions are= legal:<br> &gt;<br> &gt; enum X =3D 4;<br> &gt;<br> &gt; enum<br> &gt; =C2=A0mega =3D 1024 * 1024,<br> &gt; =C2=A0pi =3D 3.14,<br> &gt; =C2=A0euler =3D 2.72,<br> &gt; =C2=A0greet =3D &quot;Hello&quot;;<br> &gt;<br> &gt; (Both of the above enums are accepted by dmd v2.050).<br> &gt;<br> <br> </div>It&#39;s a poorly-named hack to allow people to create manifest const= ants. Ie,<br> they&#39;re like immutable values, but they don&#39;t actually take up any = space in<br> memory. In other words, it works just like C&#39;s &quot;#define SOME_VALUE= 5&quot;: the<br> value just gets substituted into the code wherever the name is found.<br></= blockquote><div><br></div><div>Yes, I understand how=C2=A0anonymous=C2=A0en= ums works. My question was regarding the D 2.0 specification document. To m= e it looks like the syntax for anonymous enums is missing in the specificat= ion. Only valid enum body definitions are empty body or a list of enum memb= ers, delimited with &#39;{&#39; abd &#39;}&#39;.</div> <div><br></div><div>/Lennart</div></div><br> --000e0cd58e681733f504a4e9b2e0--
Jun 04 2011