digitalmars.D.bugs - [Issue 4997] New: names, values, length and basetype enum properties
- d-bugmail puremagic.com (99/99) Oct 05 2010 http://d.puremagic.com/issues/show_bug.cgi?id=4997
- d-bugmail puremagic.com (10/10) May 01 2012 http://d.puremagic.com/issues/show_bug.cgi?id=4997
- d-bugmail puremagic.com (15/15) May 01 2012 http://d.puremagic.com/issues/show_bug.cgi?id=4997
- d-bugmail puremagic.com (14/14) May 27 2012 http://d.puremagic.com/issues/show_bug.cgi?id=4997
- d-bugmail puremagic.com (12/13) Jul 16 2012 http://d.puremagic.com/issues/show_bug.cgi?id=4997
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 puremagic.com ReportedBy: bearophile_hugs eml.cc 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: -------
Oct 05 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4997 Andrej Mitrovic <andrej.mitrovich gmail.com> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |andrej.mitrovich gmail.com 15:39:39 PDT --- *** Issue 8012 has been marked as a duplicate of this issue. *** -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
May 01 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4997 15:46:12 PDT --- What would really help is if you could have properties for types, something akin to: property int length(T)() if (is(T == enum)) { return __traits(allMembers, T).length; } That won't work, but that's the basic idea. Then you could put these functions into Phobos instead of complicating the language. Of course if the above work it would also complicate the language, so I dunno.. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
May 01 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4997 Two other useful properties are "succ" and "pred", to be used similarly to: enum MyEnum : ushort { FOO = 10, BAR = 20, BAZ = 40, SPAM = 30 } static assert(MyEnum.BAR.meta.succ == MyEnum.BAZ); static assert(MyEnum.BAR.meta.pred == MyEnum.FOO); -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
May 27 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4997 Tommi <tommitissari hotmail.com> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |tommitissari hotmail.comTwo other useful properties are "succ" and "pred", to be used similarly to:I'd use rather the names they chose in C++11 std lib: "next" and "prev" (stands for previous) -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jul 16 2012