Runtime type checking occurs in ActionScript 3.0 whether you compile in strict mode or standard mode. Consider a situation in which the value 3 is passed as an argument to a function that expects an array. In strict mode, the compiler will generate an error because the value 3 is not compatible with the data type Array. If you disable strict mode, and run in standard mode, the compiler does not complain about the type mismatch, but runtime type checking by Flash Player results in a runtime error. The following example shows a function
named typeTest that expects an Array argument but is passed a value of 3. This causes a runtime error in standard mode because the value 3 is not a member of the parameter’s declared data type (Array).
named typeTest that expects an Array argument but is passed a value of 3. This causes a runtime error in standard mode because the value 3 is not a member of the parameter’s declared data type (Array).
function typeTest(xParam:Array) {
trace(xParam);
}
var myNum:Number = 3;
typeTest(myNum);
// Runtime error in ActionScript 3.0 standard mode
There may also be situations where you get a runtime type error even when operating in strict mode. This is possible if you use strict mode, but opt out of compile time type checking by using an untyped variable. When you use an untyped variable, you are not eliminating type checking, but rather deferring it until runtime. For example, if the myNum variable in the previous example does not have a declared data type, the compiler cannot detect the type mismatch, but Flash Player will generate a runtime error because it compares the runtime value of myNum, which is set to 3 as a result of the assignment statement, with the type of xParam, which is set to the Array data type.
function typeTest(xParam:Array) {
trace(xParam);
}
var myNum = 3;
typeTest(myNum);
// Runtime error in ActionScript 3.0
Runtime type checking also allows more flexible use of inheritance than does compile time checking. By deferring type checking to runtime, standard mode allows you to reference properties of a subclass even if you upcast. An upcast occurs when you use a base class to declare the type of a class instance, but a subclass to instantiate it. For example, you can create a class named ClassBase that can be extended (classes with the final attribute cannot be extended):
class ClassBase {
}
You can subsequently create a subclass of ClassBase named ClassExtender, which has one property named someString.
class ClassExtender extends ClassBase {
var someString:String;
}
Using both classes, you can create a class instance that is declared using the ClassBase data type, but instantiated using the ClassExtender constructor. Upcasts are considered safe operations because the base class does not contain any properties or methods that are not in the subclass.
var myClass:ClassBase = new ClassExtender();
A subclass, however, does contain properties or methods that its base class does not. For example, the ClassExtender class contains the someString property, which does not exist in the ClassBase class. In ActionScript 3.0 standard mode, you can reference this property using the myClass instance without generating a compile-time error, as show in the following example.
var myClass:ClassBase = new ClassExtender();
myClass.someString = "hello";
// No error in ActionScript 3.0 standard mode