Flash: Cast to Array with MTASC
October 20th, 2006 by ThomasJust 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 synonymous, i.e. creating an Array with one object in it (resulting in the problem I had in my earlier post):
- var a:Array = Array({x:1});
- var b:Array = [{x:1}];
- var c:Array = new Array( {x:1});
Not for MTASC though. MTASC seems to interpret Array() as a cast operator instead of a global conversion function. Can be tricky to debug, because for FDT and MTASC Array({x:1}) is legal syntax, namely a cast operation. They don't throw an error, but they don't create an Array either. If you have more than one element they do complain. Array(1,2,3) is illegal syntax for FDT and MTASC.
The following example class would behave differently in swf's compiled with MTASC or the Flash IDE. Which strengthens my belief that the Array() operator should better not be used at all.....
- class ArrayTest
- {
- public function ArrayTest()
- {
- testArray(Array({x:1}));
- // Flash: Array.length: 1
- // MTASC: Array.length: undefined
- testArray([ {x:1} ]);
- // Both: Array.length: 1
- testArray(new Array( {x:1} ));
- // Both: Array.length: 1
- }
- private function testArray(ar:Array):Void
- {
- trace("Array.length: "+ar.length);
- }
- }



October 21st, 2006 at 7:43 pm
[…] that using (Array) does something - just something that is not casting… More on this, here.
November 10th, 2006 at 5:08 am
[…] ea is that the Array() operator can cause problems leading to unpredictable results… lessrain blog » Blog Archive » Flash: Cast to Array with MTASC public function ArrayTest() { […]