I need help understanding Dictionary and Map

While converting from AS, I translated all Objects to Dictionary (It was before I knew about the last update of Object).
Ones again, in Flash target it works perfectly, but in HTML I got a compiler error:
t : haxe.IMap<Unknown<0>, Float> -> weakKeys : Bool -> haxe.ds.StringMap<Float> should be haxe.IMap<String, Float> -> haxe.ds.StringMap<Float>

The code is this:

    private var _timers:Dictionary<String, Float>;
    public function new() {
        _timers=new Dictionary();
    }

So whats wrong with what I’ve written? And how can I understand the error message?

I know I can now switch to Object, and I’ll probably will, but I would really like to understand. I’ve looked at Dictionary source code, and it’s constructed with abstracts and stuff that I don’t fully understand yet… :sweat_smile: Thanks.

Try

_timers = new Dictionary<String, Float>();

Nope. I still get this error message:

 t : haxe.IMap<Unknown<0>, Float> -> weakKeys : Bool -> haxe.ds.StringMap<Float> should be haxe.IMap<String, Float> -> haxe.ds.StringMap<Float>

Oh, and another thing. Now that I’ve reduced the dictionary to Object, I got this compiler error:

No @:arrayAccess function accepts arguments of Int and Bool

for this line:

_downKeys[e.keyCode]=true;

I guess it has something to do with this line from Object.hx, but I don’t really understand yet:

    @:arrayAccess @:noCompletion @:dox(hide) public inline function __set (key:String, value:Dynamic):Dynamic

For what it’s worth I usually just use a Map. They are practically the same but you have to use the get and set methods instead of using it like an array. You could use your first example but just replace the word Dictionary with Map.

_downkeys[e.keyCode] = true

would become

_downkeys.set( e.keyCode, true )

Your problem above might be because set only uses strings as keys and e.keyCode is an Int. Try

_downkeys[Std.string( e.keyCode )] = true

instead.

2 Likes

You’re right. This was it. Thanks!

Or try using an Int, Bool dictionary if you are only using codes :slight_smile:

Oh, no, that’s not it, Joshua.
When using Dictionary, with any types, I got this compiler error:

 t : haxe.IMap<Unknown<0>, Float> -> weakKeys : Bool -> haxe.ds.StringMap<Float> should be haxe.IMap<String, Float> -> haxe.ds.StringMap<Float>

The workaround was using Object, which accept only String as key. :slight_smile:

What version of Haxe do you have?

I’ve downloaded and installed v3.2.1, upgraded haxelib and run openfl setup again, but still get this compiler error.

No you don’t. Maps have supported array access ever since Haxe 3.0.

Is there any small sample code I could use to reproduce the error you are receiving? Thanks!

EDIT: …and this is OpenFL 3.5.3?