AS3 - checking for undefined key in Dictionary

217 views
Skip to first unread message

Mike Stead

unread,
Apr 9, 2013, 2:00:52 AM4/9/13
to haxe...@googlegroups.com
I'm trying to create an ObjectMap which supports both primitive and object keys across targets.

For the flash impl I'd like to use a Dictionary under the hood (like haxe.ds.ObjectMap does). My issue is that in my map.exists(key) method I want to check if the key is defined. In ActionScript I'd use strict equality.

dictionary[key] !== undefined;

But I'm not sure how I can write that in haxe, even if untyped or using __global__("undefined") trickery. i.e. dictionary[key] != __global__("undefined") will return false for null too, because:

undefined == null // true
undefined === null // false

I see that the haxe.ds.ObjectMap checks for null in this case, but that won't work when the value is null but the key exists.

Reflect.hasField and the native "Object.hasOwnProperty" both deal in strings so won't work in the case of object keys.

Any way around this?

Mike Stead

unread,
Apr 9, 2013, 2:44:42 AM4/9/13
to haxe...@googlegroups.com
Actually the __in__(key, obj) works great.

var map = new flash.utils.Dictionary();
var key:Dynamic = {};
untyped map
[key] = null;
trace(untyped __in__(key, map)) // true

Juraj Kirchheim

unread,
Apr 9, 2013, 5:26:30 AM4/9/13
to haxe...@googlegroups.com
On Tue, Apr 9, 2013 at 7:00 AM, Mike Stead <stead...@gmail.com> wrote:
I'm trying to create an ObjectMap which supports both primitive and object keys across targets.
Then you shouldn't call it ObjectMap ;) 
Reply all
Reply to author
Forward
0 new messages