ActionScript 2.0 keeps the dynamic values of ActionScript 1.0 and adds type annotations, which Macromedia called strict data typing. The compiler uses them to find mistakes, then throws them away: at run time every value is as dynamic as before, and the player never checks an annotation.
Type annotations
A colon and a type name follow a variable, a parameter or a function’s parameter list:
var score:Number = 0;
var names:Array = ["Ann", "Ben"];
var hero:MovieClip = _root.hero_mc;
function label(n:Number, unit:String):String {
return n + " " + unit;
}
function reset():Void {
score = 0;
}
The type is any class or interface the compiler can find: the built-in ones, such as
Number, String, Boolean, Array,
Object, Function, MovieClip,
TextField, XML and Date, and any class on the
classpath, by its short name after an import or by its full name. Two names
are special:
Voidis only a return type, for a function that returns nothing.Objectaccepts any value; members of anObjectare not checked.
A name without an annotation is untyped and is not checked at all, as in ActionScript
1.0. There is no type inference: var n = 5 is untyped. Annotations can be
used in FLA scripts and include files as well as in class files.
What the compiler checks
- That a value assigned to a typed variable, passed to a typed parameter or returned
from a typed function has a compatible type. A subclass fits its superclass and its
interfaces, anything fits
Object, andnullandundefinedfit any type. Otherwise it reports a type mismatch, naming the type found and the type required. - That a property or method used on a typed value exists in its class, unless the
class is
dynamic. - That a function given a return type returns a value of it, and that a
Voidfunction returns none. - That the names used as types are classes or interfaces it can load, and that
privateandstaticmembers are used as declared; see Classes.
var count:Number = "three"; // error: type mismatch, String where Number is required
var t:TextField;
t.txt = "hello"; // error: TextField has no property txt
var anything = "three"; // untyped, not checked
anything.txt = "hello"; // compiles, and fails quietly when it runs
The checks reach only as far as the annotations. A value read from an untyped
variable, an array element or a timeline path such as _root.score has no
known type, so passing it on is never an error.
Casts
A cast tells the compiler to treat a value as another type. It is written like a
function call, with the type as the function: Circle(shape). In files for
Flash Player 7 and later the player checks the cast when it runs, and gives
null if the value is not an instance of that class or interface.
var s:IShape = shapes[i];
var c:Circle = Circle(s); // null if s is not a Circle
if (c != null) {
trace(c.radius);
}
The names of four built-in types are also global functions, and for them the “cast” is a conversion or a new object:
| Written | Does |
|---|---|
Number(x) | Converts x to a number. |
String(x) | Converts x to a string. |
Boolean(x) | Converts x to a boolean. |
Array(x) | Makes a new array holding x, not a cast; to treat a value as an array, assign it to an untyped variable or one typed Object first. |
Values at run time
| Type | typeof gives | Kind |
|---|---|---|
String | "string" | primitive, Unicode text |
Number | "number" | primitive, double-precision floating point; no integer type |
Boolean | "boolean" | primitive |
undefined | "undefined" | no value |
null | "null" | a deliberate empty value |
MovieClip | "movieclip" | reference |
Function | "function" | reference, including classes, which are constructor functions |
| Everything else, including buttons and text fields | "object" | reference |
instanceof tests the class of an object, and in Flash Player 7 files
also its interfaces. Primitive values are not instances: 5 instanceof Number
is false.
Conversion rules
ActionScript 2.0 is normally published for Flash Player 7 or later, and Flash Player 7 changed some conversions to follow ECMA-262. The player applies the rules of the SWF version it is given, so a class published for Flash Player 6 follows the older ones.
| Value | As a number | As a string | As a boolean |
|---|---|---|---|
undefined | NaN (Flash Player 6 files: 0) | "undefined" (Flash Player 6 files: "") | false |
null | 0 | "null" | false |
true, false | 1, 0 | "true", "false" | itself |
| a number | itself | its decimal form | false for 0 and NaN, else true |
| a string | the decimal number it spells, or with 0x a hexadecimal one; otherwise NaN | itself | true unless empty (Flash Player 6 files: true only if it converts to a number other than 0) |
| a movie clip | NaN | its path, such as "_level0.hero_mc" | true |
| another object | its valueOf() | its toString() | true |
So in a Flash Player 7 file Boolean("false") is true, and
an unset counter that is incremented becomes NaN instead of 1: typed class
members should be given starting values.
+ joins when either side is a string and adds otherwise; the other
arithmetic operators convert to numbers; == converts, ===
does not. parseInt, parseFloat, isNaN and
isFinite work as in ActionScript 1.0.