ActionScript 3.0 keeps type information at run time and checks every assignment, argument and return value against it. The compiler can also check types before the program runs, in strict mode. Type annotations are optional, but most ActionScript 3.0 code uses them everywhere.
Type annotations
A colon and a type follow the name of a variable, constant or parameter, and the parameter list of a function for its return type.
var count:int = 0;
const LABEL:String = "Score";
var anything:*; // untyped, may hold undefined
function scale(value:Number, by:Number = 2):Number {
return value * by;
}
function reset():void {
count = 0;
}
var items:Vector.<String> = new Vector.<String>();
The type is a class or interface name, *, or for a return type
void. A missing annotation means *, but in strict mode the
compiler warns about it.
Strict mode and standard mode
The compiler has two modes. Strict mode is the default in Flash authoring, Flash
Builder and the Flex and AIR compilers (-strict=true); standard mode is
-strict=false, or turning off strict mode in the publish settings. The
run-time checks are the same in both.
| Mistake | Strict mode | Standard mode |
|---|---|---|
Assigning a value of an unrelated type (var n:Number = "3") | Compile error 1067 | Converted at run time (here to 3) |
Assigning a base-class value to a subclass variable (var s:Sprite = displayObject) | Compile error 1118 | Checked at run time; error 1034 if it fails |
Using a property a sealed class does not declare (sprite.speed) | Compile error 1119 | Run-time error 1069, or undefined on a dynamic class |
| Using a name that is not declared | Compile error 1120 | Run-time error 1065 |
| Calling a method that is not declared | Compile error 1061 or 1180 | Run-time error 1006 |
| Passing the wrong number of arguments | Compile error 1136 or 1137 | Run-time error 1063 |
| Assigning a constant outside its declaration | Compile error | Allowed once in the constructor for instance constants |
Strict mode treats untyped values (*, or a value read with
[]) as unchecked: the compiler lets them through and the runtime checks
them. Standard mode exists for code written in a looser style, such as code moved from
ActionScript 2.0.
Implicit conversion
A value is converted when it is assigned, passed as an argument, returned or used
with an operator such as + or <. The rules depend on the
target type:
- Numeric types –
int,uintandNumberconvert to one another silently, even in strict mode. The result can wrap:var u:uint = -3stores 4294967293. Boolean– any value converts, by the rule in the table below, in both modes.String,Numberfrom an unrelated type – strict mode refuses it at compile time; in standard mode, or when the value is untyped, the runtime converts it by the tables below.- A class or interface type – the value must be an instance of that class, a subclass or an implementing class, or
null. Otherwise the runtime throwsTypeError1034, “Type Coercion failed”.
Explicit conversion and casts
Writing the type as a function, Type(value), converts or casts the value
on purpose, which strict mode accepts.
| Form | Result | On failure |
|---|---|---|
int(x), uint(x), Number(x), String(x), Boolean(x) | A conversion, by the tables below | 0 or NaN; never an error |
Sprite(x), IEventDispatcher(x) | The same object, now typed as that class or interface | Throws TypeError (but null passes) |
x as Sprite | The same object typed as Sprite | Gives null |
XML(x), XMLList(x) | Parses a string, or converts | Throws TypeError for null or undefined |
Array(x), Date(x) | Not casts: Array(x) makes a new array holding x, and Date() returns the current date as a string | Use x as Array or x as Date |
Testing types
| Operator | Gives | Notes |
|---|---|---|
x is T | Boolean | True when x is an instance of T, a subclass of it, or a class implementing interface T. sprite is IEventDispatcher is true. |
x as T | x or null | A cast that does not throw. The right-hand side must be a type. |
x instanceof F | Boolean | Follows the prototype chain only, as in ActionScript 2.0; it does not see interfaces, so is is preferred. |
typeof x | String | One of the values in the next table. |
| Value of type | typeof gives |
|---|---|
int, uint, Number | "number" |
String | "string" |
Boolean | "boolean" |
Function | "function" |
XML, XMLList | "xml" |
undefined | "undefined" |
Object, Array, null and every other class | "object" |
For the exact class name, flash.utils.getQualifiedClassName(x) gives a
string such as "flash.display::Sprite", and
flash.utils.describeType(x) gives the whole type as XML.
Conversion to Number, int and uint
| From | Number | int and uint |
|---|---|---|
true, false | 1, 0 | 1, 0 |
null | 0 | 0 |
undefined | NaN | 0 |
A string of a number: " 42 ", "-3.7", "0x1A" | Its value; spaces around it are ignored | Its value with the fraction cut off: int("-3.7") is −3, int("0x1A") is 26 |
Any other string, including "1 2" | NaN | 0 |
A Date | Milliseconds since 1 January 1970 UTC | The same, wrapped to 32 bits |
| Another object | Its valueOf() converted; NaN for a plain object | The same, 0 for a plain object |
A Number outside the range, or NaN | – | Wrapped to 32 bits; NaN and the infinities give 0 |
Octal is not read: Number("044") is 44, where ActionScript 2.0 gave 36.
uint("-5") is 4294967291. parseInt and
parseFloat read a number from the start of a string and stop at the first
character that does not fit.
Conversion to Boolean
| From | Gives false for | Gives true for |
|---|---|---|
Number, int, uint | 0 and NaN | Every other value |
String | null and "" | Every other string, even " " and "false" |
| Objects | null | Every object |
undefined | Always | – |
Conversion to String
| From | Gives |
|---|---|
| A number | Its decimal form: "42", "3.5", "NaN", "Infinity", "1e+21" |
true, false | "true", "false" |
null, undefined | "null", "undefined" |
An Array | The elements converted and joined with commas |
A Date | A readable date and time in local time |
An XML node | The text, for a node with simple content; otherwise the XML markup |
| Another object | Its toString(); "[object Object]" for a plain object, "[object Sprite]" for a sprite |