User Guide

66 ActionScript Language and Syntax
Implicit conversions
Implicit conversions happen at run time in a number of contexts:
In assignment statements
When values are passed as function arguments
When values are returned from functions
In expressions using certain operators, such as the addition (+) operator
For user-defined types, implicit conversions succeed when the value to be converted is an
instance of the destination class or a class that derives from the destination class. If an implicit
conversion is unsuccessful, an error occurs. For example, the following code contains a
successful implicit conversion and an unsuccessful implicit conversion:
class A {}
class B extends A {}
var objA:A = new A();
var objB:B = new B();
var arr:Array = new Array();
objA = objB; // Conversion succeeds.
objB = arr; // Conversion fails.
For primitive types, implicit conversions are handled by calling the same internal conversion
algorithms that are called by the explicit conversion functions. The following sections discuss
these primitive type conversions in detail.
Explicit conversions
It’s helpful to use explicit conversions, or casting, when you compile in strict mode because
there may be times when you do not want a type mismatch to generate a compile-time error.
This may be the case when you know that coercion will convert your values correctly at run
time. For example, when working with data received from a form, you may want to rely on
coercion to convert certain string values to numeric values. The following code generates a
compile-time error even though the code would run correctly in standard mode.
var quantityField:String = "3";
var quantity:int = quantityField; // compile time error in strict mode
If you want to continue using strict mode, but would like the string converted to an integer,
you can use explicit conversion, as follows:
var quantityField:String = "3";
var quantity:int = int(quantityField); // Explicit conversion succeeds.