Flash: Cast to Array

September 15th, 2005 by Thomas

Now that's weird: it seems to be impossible to cast to an Array in AS2!

The "natural" way of doing it for me would be this:

  1. var a:Array = [ [1, 2], [3, 4] ];
  2. var b:Array = Array(a[0]);
  3. trace(b[0]);

I'd expect the output to be "1", but it actually prints "1,2"!

After scratching my brain for a while I discovered this little paragraph in the Flash 8 help/livedoc:

You can't override primitive data types that have a corresponding global conversion function with a cast operator of the same name. This is because the global conversion functions have precedence over the cast operators. For example, you can't cast to Array because the Array() conversion function takes precedence over the cast operator.

So what it actually does in line 2, is create a new array containing the array a[0], i.e.: b = [[1,2]], resulting in the confusing output.

OK, it's a detail. Still - I discovered it when testing FDT, which (optionally) gives warnings when accessing 2-dimensional arrays like this: a[0][0] - for a reason, as there is no guarantee that a[0] is an Array!

So... no alternative, no casting to Array?

I think there should definitely be one, as it prevents you from being consistent with strict coding, unless you're using weird workarounds like this:

  1. var a:Array = [ [1, 2], [3, 4] ];
  2. var b:Array = a[0]; // accepted by fdt, as the target type is specified
  3. trace(b[0]);

instead of

  1. var a:Array = [ [1, 2], [3, 4] ];
  2. trace(Array(a[0])[0]);

It also explains this problem (the only post I found on the topic...). Java-like casting - (Array)(a[0]) - doesn't really do anything in Flash as far as I know. The only reason you're getting a result in the first example is that using (Array) does something - just something that is not casting...

More on this, here.

12 Responses to “Flash: Cast to Array”

  1. Thomas Says:

    Mr. Luis Alberto showed me how to work around it and still keep the code compact. As Objects also allow for bracket access, you can write:

    trace(Object(a[0])[0]); // “1″

    You still couldn’t do Array(a[0]).pop() or other Array specific operations though and the code would hide the fact that we’re dealing with an Array which is not the point of strict typing….

  2. Luis Says:

    Oi.. but still weird…

  3. Luis Says:

    And this is my latest surreal and not reccomendable solution:

    var a:Object = [ [1, 2], [3, 4] ];
    var b:Object = Object(a[0]);

  4. Psykovsky Says:

    Well, there’s one clean way of doing this:

    var a:Array = [[1, 2], [3, 4]];
    var b:Array = Array();
    for (i=0; i

  5. Psykovsky Says:

    oh, also you can do this in more shorter code:

    var a:Array = [[1, 2], [3, 4]];
    var b:Array = (String(a[0])).split(’,');

  6. Psykovsky Says:

    I see that prevous code is corrupted :/

    here it is:
    var a:Array = [[1, 2], [3, 4]];
    var b:Array = Array();
    for (i=0; i

  7. Psykovsky Says:

    damn html filters…

  8. BobBarker Says:

    var a:Object= [[1, 2], [3, 4]];
    var b:Array = Array(a)[0];

  9. lessrain blog » Blog Archive » Flash: Cast to Array with MTASC Says:

    […] ay with MTASC
    October 20th, 2006 by Thomas

                Just had some new insight on the Cast to Array problem I had posted a while ago.  For the Flash IDE compiler the following 3 lines are synonym [...]
  10. Sputnikenmeister Says:

    This is humiliating for Macromedia and/or the ECMA guys…

  11. Stu Says:

    I found this entry while trying to get round the same casting issue.

    I finally got around the problem by creating a new Array and concat’ing the required index of the original:

    var arr1:Array = new Array(new Array(1,2,3), new Array(4,5,6));

    var arr2:Array = new Array().concat(arr1[0]);

  12. WORMSS Says:

    I know this was some time ago, and a AS2 problem, But I thought I would just add here [as its currently high ranked in google] that in AS3 there is a solution which is

    (a[1] as Array) and you keep ur strong Casting that way such as
    (a[1] as Array).pop();
    (a[1] as Array).push(5);

Leave a Reply