Implicit enum conversions are a stupid PITA

bearophile bearophileHUGS at lycos.com
Thu Mar 25 09:28:30 PDT 2010


Ellery Newcomer:
> Somewhat OT: I wish enums had something analogous to struct's tupleof, 
> or some way to enumerate the values of the enum, and maybe their names 
> too. Do they?

D2 now gives you the tools to do that, even if it's bad looking code:

import std.stdio: writeln;
enum Foo { Aa, Bb, Cc }
void main() {
    foreach (name; __traits(allMembers, Foo))
        writeln(name, " ", mixin("cast(int)Foo." ~ name));
}


Output:
Aa 0
Bb 1
Cc 2

Bye,
bearophile



More information about the Digitalmars-d mailing list