[Issue 4997] New: names, values, length and basetype enum properties

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Oct 5 14:33:35 PDT 2010


http://d.puremagic.com/issues/show_bug.cgi?id=4997

           Summary: names, values, length and basetype enum properties
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2010-10-05 14:33:13 PDT ---
This D2 program shows how to find some info about an enum:


import std.stdio: writeln;
import std.traits: EnumMembers;

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

void main() {
    writeln("First enum member value: ", MyEnum.init);
    writeln("Smallest value of enum: ", MyEnum.min);
    writeln("Largest value of enum: ", MyEnum.max);
    writeln("Size of storage for an enumerated value: ", MyEnum.sizeof);
    writeln("Number of enum members: ", __traits(allMembers, MyEnum).length);

    static if (is(MyEnum V == enum))
        writeln("Enum Base Type: ", V.stringof);

    string[] names = [__traits(allMembers, MyEnum)];
    writeln("Enum names as strings: ", names);

    MyEnum[] values = [EnumMembers!MyEnum];
    writeln("Enum values: ", values);
}


Its output:

First enum member value: 10
Smallest value of enum: 10
Largest value of enum: 40
Size of storage for an enumerated value: 2
Number of enum members: 4
Enum Base Type: ushort
Enum names as strings: [FOO, BAR, BAZ, SPAM]
Enum values: [10, 20, 40, 30]


EnumMembers and __traits(allMembers) are useful, but they are scattered, so the
programmer has to know about them, where to find them, their syntax and usage.
It's better to have this information close where it's needed. A more handy
place to put such information is as built-in properties of all enums. So to
find that information you just need something like:


import std.stdio: writeln;

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

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


So this enhancement request asks for four enum properties, that may be named
(but other names are possible):

"length", "basetype" "names" and "values".


But keep in mind of possible name clashes (that are quite possible with the
current enum design too):

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


A possible way to avoid this problem is to *forbid* the presence of a single
member name like "meta", and then use only it to access all the enum info:


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.basetype.meta.stringof);
    writeln("Enum names as strings: ", MyEnum.meta.names);
    writeln("Enum values: ", MyEnum.meta.values);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list