Flash: Cast to Array 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 synonymous, i.e. creating an Array with one object in it (resulting in the problem I had in my earlier post):

Actionscript:
  1. var a:Array = Array({x:1});
  2. var b:Array = [{x:1}];
  3. 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.....

Actionscript:
  1. class ArrayTest
  2. {
  3.  
  4.     public function ArrayTest()
  5.     {
  6.         testArray(Array({x:1}));
  7.         // Flash: Array.length:  1
  8.         // MTASC: Array.length:  undefined
  9.    
  10.         testArray([ {x:1} ]);
  11.         // Both: Array.length:  1
  12.    
  13.         testArray(new Array( {x:1} ));
  14.         // Both: Array.length:  1
  15.     }
  16.  
  17.     private function testArray(ar:Array):Void
  18.     {
  19.         trace("Array.length: "+ar.length);
  20.     }
  21. }


2 Responses to “Flash: Cast to Array with MTASC”

  1. lessrain blog » Blog Archive » Flash: Cast to Array Says:

    […] that using (Array) does something - just something that is not casting… More on this, here.

                        This entry was posted
    
                        on Thursday, September 15th, 2005 at [...]
    
  2. Flash: Cast to Array with MTASC « Ramblings Says:

    […] 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() { […]

Leave a Reply