A type conversion is said to occur when a value is transformed into a value of a different data type. Type conversions can be either implicit or explicit. Implicit conversion, which is also called coercion, is sometimes performed by Flash Player at run time. For example, if the value 2 is assigned to a variable of the Boolean data type, Flash Player converts the value 2 to the Boolean value true before assigning the value to the variable. Explicit conversion, which is also called casting, occurs when your code instructs the compiler to treat a variable of one data type as if it belongs to a different data type. When primitive values are involved, casting actually converts values from one data type to another. To cast an object to a different type, you wrap the object name in parentheses and precede it with the name of the new type. For example, the following code takes a Boolean value and casts it to an integer:
[as]
var myBoolean:Boolean = true;
var myINT:int = int(myBoolean);
trace(myINT); // 1
[/as]
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:
[as]
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.
[/as]
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.
[as]
var quantityField:String = “3”;
var quantity:int = quantityField; // compile time error in strict mode
[/as]
If you want to continue using strict mode, but would like the string converted to an integer, you can use explicit conversion, as follows:
[as]
var quantityField:String = “3”;
var quantity:int = int(quantityField); // Explicit conversion succeeds.
[/as]
Casting to int, uint, and Number
You can cast any data type into one of the three number types: int, uint, and Number. If Flash Player is unable to convert the number for some reason, the default value of 0 is assigned for the int and uint data types, and the default value of NaN is assigned for the Number data type. If you convert a Boolean value to a number, true becomes the value 1 and false becomes the value 0.
[as]
var myBoolean:Boolean = true;
var myUINT:uint = uint(myBoolean);
var myINT:int = int(myBoolean);
var myNum:Number = Number(myBoolean);
trace(myUINT, myINT, myNum); // 1 1 1
myBoolean = false;
myUINT = uint(myBoolean);
myINT = int(myBoolean);
myNum = Number(myBoolean);
trace(myUINT, myINT, myNum); // 0 0 0
[/as]
String values that contain only digits can be successfully converted into one of the number types. The number types can also convert strings that look like negative numbers or strings that represent a hexadecimal value (for example, 0x1A). The conversion process ignores leading and trailing white space characters in the string value. You can also cast strings that look like floating point numbers using Number(). The inclusion of a decimal point causes uint() and int() to return an integer with the characters following the decimal that has been truncated. For example, the following string values can be cast into numbers.
[as]
trace(uint(“5”)); // 5
trace(uint(“-5″)); // 4294967291. It wraps around from MAX_VALUE
trace(uint(” 27 “)); // 27
trace(uint(“3.7”)); // 3
trace(int(“3.7”)); // 3
trace(int(“0x1A”)); // 26
trace(Number(“3.7”)); // 3.7
[/as]
String values that contain non-numeric characters return 0 when cast with int() or uint() and NaN when case with Number(). The conversion process ignores leading and trailing white space, but returns 0 or NaN if a string has white space separating two numbers.
[as]
trace(uint(“5a”)); // 0
trace(uint(“ten”)); // 0
trace(uint(“17 63”)); // 0
[/as]
In ActionScript 3.0, the Number() function no longer supports octal, or base 8, numbers. If you supply a string with a leading zero to the ActionScript 2.0 Number() function, the number is interpreted as an octal number, and converted to its decimal equivalent. This is not true with the Number() function in ActionScript 3.0, which instead ignores the leading zero. For example, the following code generates different output when compiled using different versions of ActionScript:
[as]
trace(Number(“044”));
// ActionScript 3.0 44
// ActionScript 2.0 36
[/as]
Casting is not necessary when a value of one numeric type is assigned to a variable of a different numeric type. Even in strict mode, the numeric types are implicitly converted to the other numeric types. This means that in some cases, unexpected values may result when the range of a type is exceeded. The following examples all compile in strict mode, though some will generate unexpected values:
[as]
var sampleUINT:uint = -3; // Assign value of type int and Number.
trace(sampleUINT); // 4294967293
var sampleNum:Number = sampleUINT; // Assign value of type int and uint.
trace(sampleNum) // 4294967293
var sampleINT:int = uint.MAX_VALUE + 1; // Assign value of type Number.
trace(sampleINT); // 0
sampleINT = int.MAX_VALUE + 1; // Assign value of type uint and Number.
trace(sampleINT); // -2147483648
[/as]
Casting to Boolean
Casting to Boolean from any of the numeric data types (uint, int, and Number) results in false if the numeric value is 0, and true otherwise. For the Number data type, the value NaN also results in false. The following example shows the results of casting the numbers -1, 0, and 1:
[as]
var myNum:Number;
for (myNum = -1; myNum<2; myNum++)
{
trace(“Boolean(” + myNum +”) is ” + Boolean(myNum));
}
[/as]
The output from the example shows that of the three numbers, only 0 returns a value of false:
[as]
Boolean(-1) is true
Boolean(0) is false
Boolean(1) is true
[/as]
Casting to Boolean from a String value returns false if the string is either null or an empty string (“”). Otherwise, it returns true.
[as]
var str1:String; // Uninitialized string is null.
trace(Boolean(str1)); // false
var str2:String = “”; // empty string
trace(Boolean(str2)); // false
var str3:String = ” “; // white space only
trace(Boolean(str3)); // true
[/as]
Casting to Boolean from an instance of the Object class returns false if the instance is null, and true otherwise, as the following example shows:
[as]
var myObj:Object; // Uninitialized object is null.
trace(Boolean(myObj)); // false
myObj = new Object(); // instantiate
trace(Boolean(myObj)); // true
[/as]
Boolean variables get special treatment in strict mode in that you can assign values of any data type to a Boolean variable without casting. Implicit coercion from all data types to the Boolean data type occurs even in strict mode. In other words, unlike almost all other data types, casting to Boolean is not necessary to avoid strict mode errors. The following examples all compile in strict mode and behave as expected at run time:
[as]
var myObj:Object = new Object(); // instantiate
var bool:Boolean = myObj;
trace(bool); // true
bool = “random string”;
trace(bool); // true
bool = new Array();
trace(bool); // true
bool = NaN;
trace(bool); // false
[/as]
Casting to String
Casting to the String data type from any of the numeric data types returns a string representation of the number. Casting to the String data type from a Boolean value returns the string “true” if the value is true, and returns the string “false” if the value is false.
Casting to the String data type from an instance of the Object class returns the string “null” if the instance is null. Otherwise, casting to the String type from the Object class returns the string “[object Object]”.
Casting to String from an instance of the Array class returns a string comprising a comma-delimited list of all the array elements. For example, the following cast to the String data type returns one string containing all three elements of the array:
[as]
var myArray:Array = [“primary”, “secondary”, “tertiary”];
trace(String(myArray)); // primary,secondary,tertiary
[/as]
Casting to String from an instance of the Date class returns a string representation of the date that the instance contains. For example, the following example returns a string representation of the Date class instance (the output shows result for Pacific Daylight Time):
[as]
var myDate:Date = new Date(2005,6,1);
trace(String(myDate)); // Fri Jul 1 00:00:00 GMT-0700 2005
[/as]