MXMLC WTF (8): Keys can be of any type as long as it’s String

Have you ever tried looping over all the keys in a Dictionary? The whole point of the Dictionary class is that you can have objects of any type as keys, unfortunately someone forgot to tell the ActionScript compiler that:

var dict : Dictionary = ...;

for ( var k : MySpecialType in dict ) { trace(k, dict[k]); }

Error: Implicit coercion of a value of type String to an unrelated type

WTF?

6 Responses to “MXMLC WTF (8): Keys can be of any type as long as it’s String”

  1. Josh M Says:

    Unfortunately a side-effect of Flash.utils.Proxy:

    nextName(index:int):String;

    -Josh

  2. Robert Penner Says:

    It looks like you want to use a “for each” loop.

  3. Theo Says:

    @Robert

    I think it’s pretty obvious that I’m talking about looping over the keys of dictionaries, for-each loops are for looping over the values.

    @Josh

    Good spotting, that must be the root of the issue. This means that the error is not in the compiler but in the Flash API, or a combination of both, since the compiler has to be aware of flash.utils.Proxy.

  4. Robert Penner Says:

    Sorry, I misunderstood. So how did you get around this issue? Did you type k as Object or *?

  5. Theo Says:

    I type as * it saves me from casting. It’s not a common issue, but an annoying one.

  6. Josh McDonald Says:

    On further messing about, it seems it’s not the API’s fault, but MXMLC. It isn’t smart enough to know that subclasses of Object can only be indexed by QNames except Dictionary.

Leave a Reply