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;
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 loop through an enum C ++? Yes. It iterates over an std::initializer_list<Item>.
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.
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.
What you are looking for is Type.allEnums()
:
for (prop in Type.allEnums(PlayerProps))
Working example on try.haxe.org.
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