www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Enum proprierties

The current way enums are designed may cause name clashes that the DMD ignores:

enum SillyEnum {
    init,
    length,
    max,
    min,
}


And to find some basic info about an enum you have to use not nice things as
EnumMembers and __traits(allMembers) scattered around.

So here I have suggested to disallow a single field name like "meta" or "info"
and use it to access enum information:
http://d.puremagic.com/issues/show_bug.cgi?id=4997

Example:


import std.stdio: writeln;

enum MyEnum : ushort {
    FOO  = 10,
    BAR  = 20,
    BAZ  = 40,
    SPAM = 30
}

void main() {
    writeln("First enum member value: ", MyEnum.meta.init);
    writeln("Smallest value of enum: ", MyEnum.meta.min);
    writeln("Largest value of enum: ", MyEnum.meta.max);
    writeln("Size of storage for an enumerated value: ", MyEnum.meta.sizeof);
    writeln("Number of enum members: ", MyEnum.meta.length);
    writeln("Enum Base Type: ", MyEnum.meta.basetype.stringof);  // was wrong
in bug 4997
    writeln("Enum names as strings: ", MyEnum.meta.names);
    writeln("Enum values: ", MyEnum.meta.values);
}

Bye,
bearophile
Oct 05 2010