Does D provide some way of reflecting on the values of an enumeration, say e
, typically iterating over each of its values a bit similar to .tupleof
for structs and classes. I guess we could always do
foreach (val; e.min..e.max)
but what about non-continuous enumerations? And enumerations are inclusive on max-value but ranges are not.
Update: I just discovered: http://dlang.org/phobos/std_traits.html#.EnumMembers
I believe this all we need right?
Enumerations make for clearer and more readable code, particularly when meaningful names are used. The benefits of using enumerations include: Reduces errors caused by transposing or mistyping numbers. Makes it easy to change values in the future.
An enumerated type is a type whose legal values consist of a fixed set of constants. Common examples include compass directions, which take the values North, South, East and West and days of the week, which take the values Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday.
In Swift, an enum (short for enumeration) is a user-defined data type that has a fixed set of related values. We use the enum keyword to create an enum. For example, enum Season { case spring, summer, autumn, winter } Here, Season - name of the enum.
Advantages of Enumerated Data Type:Makes program more readable to the fellow developers. Makes program more manageable while dealing enum type of variables. Using enumerators reduces programming error.
std.traits.EnumMembers
can do it, and so can __traits(allMembers,YourEnum)
.
foreach(value, name; EnumMembers!YourEnum) { }
sounds like what you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With