Re: how does the haxe3 new map works?

41 views
Skip to first unread message
Message has been deleted

Rudy G

unread,
Feb 19, 2014, 7:03:48 AM2/19/14
to haxe...@googlegroups.com

Hi,

Map needs two types : one for the key, one for the content.

For example, if you want a Map containing integers with strings as keys, you have to create it this way:

var map = new Map<String,Int>();


(well, there's StringMap for this example :P)

davy zhang

unread,
Feb 19, 2014, 7:14:56 AM2/19/14
to haxe...@googlegroups.com
Hi, 

says:

I know I should put them together just like 
var map = new Map();
map["xxx"] = "yyy"; //will compile

also this works

var m:Map<String,String>; 
m = new Map();
m["ss"] = "mm";

Thanks~ 

As a bonus, maps allow element access with the Array operator:

var map = new Map();
map["foo"] = 9;
trace(map["foo"]); // 9
map["foo"] += 12;
trace(map["foo"]); // 21

davy zhang

unread,
Feb 19, 2014, 7:14:58 AM2/19/14
to haxe...@googlegroups.com
Hi, 

says:

I know I should put them together just like 
var map = new Map();
map["xxx"] = "yyy"; //will compile

also this works

var m:Map<String,String>; 
m = new Map();
m["ss"] = "mm";

Thanks~ 

As a bonus, maps allow element access with the Array operator:

var map = new Map();
map["foo"] = 9;
trace(map["foo"]); // 9
map["foo"] += 12;
trace(map["foo"]); // 21

On Wednesday, February 19, 2014 8:03:48 PM UTC+8, Rudy G wrote:

Rudy G

unread,
Feb 19, 2014, 7:27:29 AM2/19/14
to haxe...@googlegroups.com
When you create a Map using new Map(); (without <Type1,Type2>), the compiler adds those types using what you do with the map.

So when you do m["ss"] = "mm"; later, it assumes your map is a Map<String,String>

Using the new Map(); alone without doing anything with your map does not tel the compiler what kind of Map it is, so it fails

Reply all
Reply to author
Forward
0 new messages