O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Strict Mode's Three Special Cases

There are three situations in which the compiler ignores type mismatch errors in strict mode, deferring possible type errors until runtime:

  • When an untyped expression is assigned to a typed variable or parameter, or returned from a function with a declared return type

  • When any expression is assigned to a typed variable or parameter whose declared type is Boolean, or returned from a function whose declared return type is Boolean

  • When any numeric value is used where an instance of a different numeric type is expected

Let's look at each of the preceding cases with an example. First, we'll create an untyped variable, pet, and assign the value of that variable to a typed variable, d:

var pet:* = new VirtualPet("Francis");
pet = new Date();
var d:Date = pet;

Because pet can contain any type of value, on line 3, the compiler cannot determine whether pet's value belongs to the datatype Date. To determine whether the value in pet belongs to the datatype Date, the code must be executed, not just compiled. Once the code is actually executing, ActionScript can then determine the result of the assignment attempt. In the case of the preceding code, the value in pet (assigned on line 2) does indeed belong to the datatype Date (even though pet's value on line 1 was originally incompatible with Date). Hence, the assignment proceeds without causing an error.

Next, consider the following code which defines a variable, b, of type Boolean, and assigns b an integer value, 5: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required