Haxe structure vs map

(Anton) #1

Hi! Haxe newbie here. I’m trying to implement a structure that will hold a basic mapping from keyboard key functionalities to their appropriate names (eg jump => w, move left => a, etc), but can’t decide on whether to use a map or a structure for this. Coming from Javascript I see a lot of similarities between this issue and the Map vs Object question, but I don’t know if it applies. Can anyone give me some tips on when to use these two collection types? Thank you!

(Jérémy Faivre) #2

I guess if your keys are String instances, both are good options and got some pros and cons:

  • If your mapping never changes, it’s a good idea to use a structure as it will provide compile-time type information when you use it and warn you when you try to read a key that doesn’t exist on the mapping.
static var mapping = {
    jump: 'w',
    moveLeft: 'a'
  • On the other hand, if your mapping has dynamic keys that you won’t know in advance, you may prefer to use a Map, but this won’t provide any hint or code completion about the available keys when you type in your code mapping.|

I would also add that structures play well with JSON while Maps don’t out of the box. But this is very similar to what you would have in javascript and mostly depends on what you need to do!