Dictionaries are very useful. Unlike Objects, your keys don’t need to be Strings. But there are remnants of this String requirement that are not obvious. Fortunately, it’s a simple rule to remember…

Reader golgobot commented recently about this issue. The essence of the issue is that Dictionary keys are converted:

var dict:Dictionary = new Dictionary();
dict[3] = "hello";
trace(dict["3"]);

This prints “hello”. Now to reveal the answer for his second post. Here was the code:

var dict:Dictionary = new Dictionary();
dict[null] = "null";
dict[undefined] = "undefined";
dict[NaN] = "NaN";
dict[int] = "int";
 
for (var i:* in dict)
{
	trace("dict["+i+"] = " + dict[i]);
}
 
for each (var j:* in dict)
{
	trace("j = " + j);
}

And the answer is…

NaN String, null String, Dictionary real

As you see, the conversion holds true until you have an object that isn’t a basic type like int, Number, String, and so forth. Even at the level of objects like Dictionary, you can expect unique keys.

Thanks to golgobot for commenting!