Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enumeration Reflection

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?

like image 983
Nordlöw Avatar asked Nov 05 '13 21:11

Nordlöw


People also ask

What is the purpose of using enumeration?

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.

What is enumeration explain with example?

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.

What is enumeration in Swift explain with example?

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.

What are the advantages of enumerated data types?

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.


1 Answers

std.traits.EnumMembers can do it, and so can __traits(allMembers,YourEnum).

foreach(value, name; EnumMembers!YourEnum) { } sounds like what you want.

like image 156
Adam D. Ruppe Avatar answered Nov 15 '22 09:11

Adam D. Ruppe