Creating map using templated type T as key type

115 views
Skip to first unread message

Jason Hobbs

unread,
Jun 26, 2013, 10:26:46 AM6/26/13
to haxe...@googlegroups.com
Hey guys,

I'm updating some codes from Haxe 2.10 to Haxe 3 and I'm struggling a bit here with the new Maps and/or generic stuff!

Say I have a generic class which contains the member maps as follows.

class DAG<T>
{
var m:Map<T, String >;
var m1:Map<String, T>;

public function new()
{
                // error: Abstract Map has no @:to function that accepts IMap<utility.DAG.T, Array<utility.DAG.T>>
m = new Map<T, String>();
// compiles fine
m1 = new Map<String, T>();
}
}

This will compile error with the error in the comment if T is used as the key type however it works fine as the value Type.
Any suggestions how I can pass a type into DAG to be the key type of the map?

Thanks
J

Jason Hobbs

unread,
Jun 26, 2013, 10:34:50 AM6/26/13
to haxe...@googlegroups.com
Just to add that i have 
@:generic class DAG<T>

Joshua Granick

unread,
Jun 26, 2013, 12:57:40 PM6/26/13
to haxe...@googlegroups.com
I only got this to work by hard-coding IntMap or StringMap rather than the Map abstract :(

TopHattedCoder

unread,
Jun 26, 2013, 6:17:05 PM6/26/13
to haxe...@googlegroups.com
I think it's because the abstract functions for conversion between a Map and ObjectMap use a {} for the key instead of Dynamic, try constraining the parameter to {}, like so:
class DAG<T:{}> {
 
var m:Map<T, String >;
 
var m1:Map<String, T>;
 
public function new() {

Jason Hobbs

unread,
Jun 26, 2013, 6:28:54 PM6/26/13
to haxe...@googlegroups.com
Awesome thanks man, That works like a charm!

Joshua Granick

unread,
Jun 27, 2013, 1:43:16 AM6/27/13
to haxe...@googlegroups.com
What exactly does this syntax mean?

Stephane Le Dorze

unread,
Jun 27, 2013, 3:31:59 AM6/27/13
to haxe...@googlegroups.com
Works indeed but one needs to understand that it will not select the most specific implementation - for instance if T is a String.

Simon Krajewski

unread,
Jun 27, 2013, 4:37:43 AM6/27/13
to haxe...@googlegroups.com
Am 27.06.2013 09:31, schrieb Stephane Le Dorze:
> Works indeed but one needs to understand that it will not select the
> most specific implementation - for instance if T is a String.

Considering @:generic, the real problem is that the compiler tries to
post-process the unapplied version of the class and then encounters the
error. This would be this issue:
https://github.com/HaxeFoundation/haxe/issues/1578

The concrete classes should actually use StringMap/EnumValueMap if the
type parameter matches. This should be tested though.

Simon

Jason Hobbs

unread,
Jun 27, 2013, 7:42:55 AM6/27/13
to haxe...@googlegroups.com
So if T is a string in this example it will still use ObjectMap as opposed to StringMap?

Nicolas Cannasse

unread,
Jun 28, 2013, 4:24:34 PM6/28/13
to haxe...@googlegroups.com
Le 27/06/2013 07:43, Joshua Granick a �crit :
> What exactly does this syntax mean?

>>> class DAG<T:{}> {

It means that "T" should be at least the empty structure {}, so any kind
of object will work.

Best,
Nicolas

Joshua Granick

unread,
Jun 28, 2013, 4:26:51 PM6/28/13
to haxe...@googlegroups.com, Nicolas Cannasse
So it prevents it from being null or Void?


On Fri, 28 Jun 2013 13:24:34 -0700, Nicolas Cannasse <ncan...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages