COMMUNITY

Generic multi-map

I’m trying to implement generic multi-map in Haxe and this is what I’ve got so far:

https://try.haxe.org/#E2f86

Error: Abstract MultiMap has no @:to function that accepts Map<String, Array> is confusing me (shouldn’t @:to function have NO arguments??) and I have no idea what to do next.

I have tried creating MultiMap class which encapsulates Map<K, Array> but in that case constructor like this:

class MultiMap< K, V > {
...
public function new() {
    this.map = new Map<K, Array<V>>();
}

doesn’t compile.

I don’t have to use abstract for generic multi-map, if someone has a better idea, please post it here.

The only other option I see now is to code specialized multi-map class for each combination of K, V I have in my code :slightly_frowning_face:

Not when it’s a @:multiType abstract that has to be specialized to a concrete implementation. Have a look at the implementation of Map, you can probably get away with defining @:to functions analogous to those.

When I’m in a supermarket and spend 10-15 minutes looking for something, I usually go and find someone who works there and ask them where they’re keeping such-and-such thing. More often than not, they just point at the shelf right behind me and say: “It’s there”. :joy:

I did look at the source of Map, but didn’t scroll all the way to bottom where you can find @:to functions for concrete casts.

Thanks Jens! :v:

Here is fixed and working multi-map for String keys, adding other @:to functions will cover the rest of the key types:

https://try.haxe.org/#5DDcc

Nice! Might I suggest that you name that method add() rather than set() though? The latter seems rather misleading considering what it does. :slight_smile: