View comments | RSS feed

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 (see www.mozilla.org/js/language/es4/index.html). 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 but share a base type.

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 at runtime (in files published for Flash Player 7 or later; files published for Flash Player 6 do not have runtime support for failed casts). If the cast succeeds, the function call returns the original object. However, the compiler cannot determine whether a cast will fail at runtime and won't generate compile-time errors in those cases. The following code shows an example:

function bark(myAnimal:Animal) {
var foo:Dog = Dog(myAnimal);
foo.bark();
}

var curAnimal:Animal = new Dog();
bark(curAnimal); // will work
curAnimal = new Cat();
bark(curAnimal); // won't work 

In this situation, you asserted to the compiler that foo is a Dog object, and, therefore, the compiler assumes that temp.bark(); is a legal statement. However, the compiler doesn't know that the cast will fail (that is, that you tried to cast a Cat object to an Animal 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 casting errors at runtime.

import Dog;
function bark(myAnimal:Animal) {
   var foo:Dog = Dog(myAnimal);
   if (foo) {
      foo.bark();
   }
}

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 cast succeeds. If not, the cast fails.

Casting to null or undefined returns undefined.

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. For more information on data conversion functions, see the entry for each conversion function in Flash ActionScript Language Reference: Array(), Boolean(), Number(), Object(), String().


Comments


senocular said on Mar 22, 2005 at 3:35 AM :
Third paragraph, temp.bark(); should read foo.bark();
jepo said on Apr 28, 2005 at 11:34 AM :
Thanks! I've updated the source files.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/mx2004/main_7_2/00000781.html