ActionScript 3 Casting

| 3 Comments | 3 TrackBacks

If you're anything like me, you're not a fan of how ActionScript 2 handles casting. Luckily, ActionScript 3 introduces a new operator to make casting more intuitive.

The syntax for casting a from one datatype to another is Type( expression ) - see About casting objects. There are a few reasons why this syntax is confusing:

  • Not consistent with other languages - Both Java and C# use the syntax (Type) expression.
  • Ambiguous syntax - Because the cast looks like the call to a constructor, the code isn't as clear as it could be. Did you simply forget the new keyword, or did you actually want to perform a cast?
  • Conversion operator precedence - ActionScript includes conversion functions such as Array(), Number(), and Boolean() that take precedence over a cast and usually yield unexpected results. It's impossible to cast to an Array because the conversion function is called instead.

To address these issues, ActionScript 3 introduces the as operator which behaves very similar to a cast. The syntax is expression as datatype, and is exactly the same syntax that C# uses, which should aid developers in migrating to ActionScript 3 in the future.

Using the as operator is simple:

// The getChildAt method returns a DisplayObject, but
// we want to cast it to a Sprite, so we use the as operator
var s:Sprite = getChildAt(0) as Sprite;

A key difference between casting and the as operator is the behavior on failure. When a cast fails in ActionScript 2, null is returned. When a cast fails in ActionScript 3, a TypeError is thrown. With the as operator in ActionScript 3, whenever a cast fails the default value for the datatype is returned. See the documentation for more information.

var o:Object = "test";

trace( o as MovieClip ); // displays: null
trace ( MovieClip ( o ) ); // generates a TypeError

One of the nice benefits of using as over a cast is that you can successfully cast to the Array datatype now:

// Create a generic Object but initialize it with 
// an Array for demonstration purposes
var o:Object = [1, 2, 3];

// Attempt to cast o as an Array - unexpected results
// because the Array conversion function is called instead
var a:Array = Array( o );
// The Array conversion function creates an array with a single
// element containg the value of o
trace( a.length ); // displays: 1
// Demonstrate that a contains a single element (an array of length 3)
trace( a[0].length ); // displays: 3
// Access the first element from o
trace( a[0][0] ); // displays: 1

// Cast o to an Array via the as keyword - this works like
// we expect it to
var b:Array = o as Array;
trace( b.length ); // displays: 3
// Access the first element from o
trace( b[0] ); // displays: 1

Positive changes like this really make ActionScript 3 enjoyable to work with. I don't think I'll ever understand why casts were originally defined as Type( expression ) when ( Type ) expression seems superior, but at least the as operator makes casting tolerable again.

3 TrackBacks

TrackBack URL: http://www.darronschall.com/mt/mt-tb.cgi/93

Flex2 ?????ActionScript 3 ???? Read More

Cairngorm Responder Interface Change from Jesse Warden - Flash, Flex, and Component Developer on March 11, 2006 12:18 PM

Doing some re-factoring today in a Flex 1.5 project, and realized a lot of our Commands & Delegates we're implementing the Cairngorm's Responder interface, but using vanilla (plain) Objects for the onResult and onFault functions. While that is the corr... Read More

Cairngorm Responder Interface Change from Jesse Warden - Flash, Flex, and Component Developer on March 11, 2006 12:18 PM

Doing some re-factoring today in a Flex 1.5 project, and realized a lot of our Commands & Delegates we're implementing the Cairngorm's Responder interface, but using vanilla (plain) Objects for the onResult and onFault functions. While that is the corr... Read More

3 Comments

  • That's definately useful.

    Do you think this will change before the release.

    As yousaid it follows C++ syntax, is there no equivelent in java? Most AS seems closer to java.

    Maybe a change in direction for actionscript?

     
  • i think both the casting syntax and the method signature syntax is regretable. a futher gripe i have with the flex (<2) and as2 compiler is that tehre was no strict mode (which made MTASC so usefull)

    as3 has really opened new doors in many of these regards but the method signature should have changed IMO

     
  • Ah, the debate over post-colon syntax. Personally, I don't think the typing syntax matters too much. After working with it for so long, you just grow accustomed to it. It reminds me of my days of working with Pascal (which used ": type" for both variables and return types).

    The casting syntax is regrettable because of the problems I mentioned. The typing syntax is just a matter of style (though it is inconsistent with some languages like C# and Java, it is still consistent with Delphi/Pascal).

     

Leave a comment

Flex.org - The Directory for Flex

Archives