Casting objects

ActionScript 2.0 lets you cast one data type to another. The cast operator that Flash uses takes the form of a function call and is concurrent with explicit coercion, as specified in the ECMA-262 Edition 4 proposal. Casting lets you assert that an object is of a certain type so that when type-checking occurs, the compiler treats the object as having a set of properties that its initial type does not contain. This can be useful, for example, when iterating over an array of objects that might be of differing types.

In files published for Flash Player 7 or later, cast statements that fail at runtime return null. In files published for Flash Player 6, no runtime support for failed casts is implemented.

The syntax for casting is type(item), where you want the compiler to behave as if the data type of item is type. Casting is essentially a function call, and the function call returns null if the cast fails. If the cast succeeds, the function call returns the original object. However, the compiler doesn't generate type mismatch errors when you cast items to data types that you created in external class files, even if the cast fails at runtime.

// in Animal.as
class Animal {}

// in Dog.as 
class Dog extends Animal { function bark (){} }

// in Cat.as 
class Cat extends Animal { function meow (){} }

// in FLA file
var spot:Dog = new Dog();   
var temp:Cat = Cat (spot);  // assert that a Dog object is of type Cat
temp.meow(); // doesn't do anything, but no compiler error either

In this situation, you asserted to the compiler that temp is a Cat object, and, therefore, the compiler assumes that temp.meow() is a legal statement. However, the compiler doesn't know that the cast will fail (that is, that you tried to cast a Dog object to a Cat type), so no compile-time error occurs. If you include a check in your script to make sure that the cast succeeds, you can find type mismatch errors at runtime.

var spot:Dog = new Dog();
var temp:Cat = Cat (spot);
trace(temp);  // displays null at runtime

You can cast an expression to an interface. If the expression is an object that implements the interface or has a base class that implements the interface, the object is returned. If not, null is returned.

The following example shows the results of casting built-in object types. As the first line in the with(results) block shows, an illegal cast—in this case, casting a string to a movie clip—returns null. As the last two lines show, casting to null or undefined returns undefined.

var mc:MovieClip;
var arr:Array;
var bool:Boolean;
var num3:Number;
var obj:Object;
var str:String;
_root.createTextField("results",2,100,100,300,300);
with(results){
text = "type MovieClip : "+(typeof MovieClip(str));     // returns null
text += "\ntype object : "+(typeof Object(str));        // returns object
text += "\ntype Array : "+(typeof Array(num3));         // returns object
text += "\ntype Boolean : "+(typeof Boolean(mc));       // returns boolean
text += "\ntype String : "+(typeof String(mc));         // returns string
text += "\ntype Number : "+(typeof Number(obj));        // returns number
text += "\ntype Function : "+(typeof Function(mc));     // returns object
text += "\ntype null : "+(typeof null(arr));            // returns undefined
text += "\ntype undefined : "+(typeof undefined(obj));  // returns undefined
}
//Results in Output panel
type MovieClip : null
type object : object
type Array : object
type Boolean : boolean
type String : string
type Number : number
type Function : object
type null : undefined
type undefined : undefined

You can't override primitive data types such as Boolean, Date, and Number with a cast operator of the same name.