15

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;
Gama11
  • 31,714
  • 9
  • 78
  • 100
meps
  • 579
  • 2
  • 17

1 Answers1

19

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

for (prop in Type.allEnums(PlayerProps))

Working example on try.haxe.org.

Gama11
  • 31,714
  • 9
  • 78
  • 100
npretto
  • 1,098
  • 7
  • 18
  • Thanks, I've found allEnums a bit later too. But now I have learned that Map can't use enum type as a key... – meps Mar 19 '14 at 20:46
  • Oh, my bad for not testing that, I think you could find a workaround by using the int-value of the enum (it should be accessible somehow) but I guess that is not the best solution for this. Have you considered creating an object instead of a map? You could define a class that as a field for each property (this may or may not work for your needs) – npretto Mar 19 '14 at 20:49
  • I'm trying to find workaround with IntMap and Type.enumIndex method. But this looks quite clumsy. – meps Mar 19 '14 at 21:00
  • 4
    @meps Enums in maps should work, they should use: [haxe.ds.EnumValueMap](http://api.haxe.org/haxe/ds/EnumValueMap.html). If you have issues try type it as `var mProps:EnumValueMap = new EnumValueMap()` and see if that makes a difference... – Jason O'Neil Mar 20 '14 at 00:24