Can someone explain this?

Jeff massung at gmail.com
Sat Feb 6 14:39:38 UTC 2021


I'm trying to get the values of an enum at compile-time and 
running into a behavior I don't understand.

Consider the following enum:

enum A {x="foo", y="bar"}

And now, I just want to print out the values of A at runtime 
(e.g. A.x = "foo").

void main()
{
     static foreach(i, op; EnumMembers!A)
     {
         writeln("A." ~ op.to!string ~ " = " ~ op);
     }
}

Okay, the above works. But, I'm not sure why?

If I just do writeln(op), it prints out x and y, which is the 
same if I do op.to!string. If I use a mixin... mixin("\"" ~ op ~ 
"\"") then it prints foo and bar.

So, I'm guessing there's something going on under-the-hood using 
the ~ operator with the enum and I'd like to understand what it 
is.

Likewise, if there's an easier method of getting the "value of 
enum" I haven't discovered yet, that'd be just as nice to know. ;)

Thanks!


More information about the Digitalmars-d-learn mailing list