Wrapping Map<K, T>

45 views
Skip to first unread message

James Hofmann

unread,
Mar 7, 2013, 3:26:05 AM3/7/13
to haxe...@googlegroups.com
class MapWrapper<K, T>
{

public var m : Map<K, T>;
public function new()
{
m = new Map<K, T>();
}

}

src/test/Main.hx:17: characters 6-21 : Abstract Map has no @:to function that accepts _Map.IMap<test.MapWrapper.K, test.MapWrapper.T>

I think there is a way to do it by making my wrapper class an Abstract and following something similar to what Map itself does, but I'm a bit confused by the syntax and there aren't many examples, so I'm asking the list for some guidance.

Simon Krajewski

unread,
Mar 7, 2013, 3:31:44 AM3/7/13
to haxe...@googlegroups.com
Map is a multitype abstract, which means that it choses a concrete type based on its type parameters. These choices are defined by the @:to functions here:
    https://code.google.com/p/haxe/source/browse/trunk/std/Map.hx#126
The compiler will check them from top to bottom, and the first one whose t argument-type matches is selected.

By extension, this explains why your example above does not work. The type parameters there are not known, so the compiler does not know which implementation to pick. I have plans to support this if MapWrapper is a @:generic class, but not yet in haxe 3.

Simon

James Hofmann

unread,
Mar 7, 2013, 4:20:24 AM3/7/13
to haxe...@googlegroups.com
Okay. The main source of difficulty for me is really that I want to have the key type be used within the wrapper's algorithms(because it's also linked to another data structure). I've been running through the gamut of possibilities and it looks like copy-paste - or the macro form of it - is going to be the most straightforward solution.

Simon Krajewski

unread,
Mar 7, 2013, 4:48:33 AM3/7/13
to haxe...@googlegroups.com
If you know that K is an class type, you may use haxe.ds.ObjectMap directly.

Simon

Juraj Kirchheim

unread,
Mar 7, 2013, 5:10:58 AM3/7/13
to haxe...@googlegroups.com
On Thu, Mar 7, 2013 at 9:48 AM, Simon Krajewski <si...@haxe.org> wrote:
[...]
> If you know that K is an class type, you may use haxe.ds.ObjectMap directly.

Either that, or just pass the Map into the constructor, rather than
creating it there.

Regards,
Juraj
Reply all
Reply to author
Forward
0 new messages