Comment #0 by bearophile_hugs — 2010-10-05T14:33:13Z
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);
}
Comment #1 by andrej.mitrovich — 2012-05-01T15:39:39Z
*** Issue 8012 has been marked as a duplicate of this issue. ***
Comment #2 by andrej.mitrovich — 2012-05-01T15:46:12Z
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..
Comment #3 by bearophile_hugs — 2012-05-27T10:56:27Z
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);
Comment #4 by tommitissari — 2012-07-16T10:02:50Z
(In reply to comment #3)
> Two 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)
Comment #5 by lt.infiltrator — 2014-03-19T03:56:10Z
*** Issue 5099 has been marked as a duplicate of this issue. ***
Comment #6 by dmitry.olsh — 2018-05-18T09:24:36Z
Could be easily superseeded by coming introspection DIP
Comment #7 by robert.schadek — 2024-12-13T17:53:39Z