Flash: Cast to Array
September 15th, 2005 by ThomasNow 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:
- var a:Array = [ [1, 2], [3, 4] ];
- var b:Array = Array(a[0]);
- 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:
- var a:Array = [ [1, 2], [3, 4] ];
- var b:Array = a[0]; // accepted by fdt, as the target type is specified
- trace(b[0]);
instead of
- var a:Array = [ [1, 2], [3, 4] ];
- 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...



September 15th, 2005 at 6:44 pm
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….
September 15th, 2005 at 6:46 pm
Oi.. but still weird…
September 15th, 2005 at 7:14 pm
And this is my latest surreal and not reccomendable solution:
Object.prototype=Array.prototype;
var a:Object = [ [1, 2], [3, 4] ];
var b:Object = Object(a[0]);
trace(b.pop());
September 25th, 2005 at 2:08 am
Well, there’s one clean way of doing this:
var a:Array = [[1, 2], [3, 4]];
var b:Array = Array();
for (i=0; i
September 25th, 2005 at 2:10 am
oh, also you can do this in more shorter code:
var a:Array = [[1, 2], [3, 4]];
var b:Array = (String(a[0])).split(’,');
trace(b[0]);
September 25th, 2005 at 2:11 am
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
September 25th, 2005 at 2:12 am
damn html filters…
May 9th, 2006 at 9:26 pm
var a:Object= [[1, 2], [3, 4]];
var b:Array = Array(a)[0];
October 20th, 2006 at 4:37 pm
[…] ay with MTASC
October 20th, 2006 by Thomas
April 19th, 2007 at 10:47 pm
This is humiliating for Macromedia and/or the ECMA guys…
November 27th, 2008 at 4:38 pm
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]);
July 22nd, 2009 at 12:31 pm
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);