Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I iterate over an enum in Haxe?

I have an enumerator type:

enum PlayerProps {
    Attempts;
    Gold;
    Diamonds;
}

What should I do to iterate through all enum values? Something like:

var props = new Map<PlayerProps, Int>();
for (prop in PlayerProps)
    props[prop] = 0;
like image 684
meps Avatar asked Mar 19 '14 19:03

meps


People also ask

Can you iterate over an enum?

Enums don't have methods for iteration, like forEach() or iterator(). Instead, we can use the array of the Enum values returned by the values() method.

Can you iterate through an enum class C ++?

Can you loop through an enum C ++? Yes. It iterates over an std::initializer_list<Item>.

Can you iterate through an enum in C?

you can iterate the elements like: for(int i=Bar; i<=Last; i++) { ... } Note that this exposes the really-just-an-int nature of a C enum. In particular, you can see that a C enum doesn't really provide type safety, as you can use an int in place of an enum value and vice versa.

Can we loop through enum Java?

Using for loopYou can retrieve the contents of an enum using the values() method. This method returns an array containing all the values. Once you obtain the array you can iterate it using the for loop.


1 Answers

What you are looking for is Type.allEnums():

for (prop in Type.allEnums(PlayerProps))

Working example on try.haxe.org.

like image 172
npretto Avatar answered Oct 25 '22 00:10

npretto