knowledge.lapasa.net

Greater Toronto Area Flash Platform Developer Blog

Type Conversions: Coercion vs. Casting

TAGS: None

Coercion
At run-time the Flash Player will implicitly convert the data type of a value into one that is suitable to be assigned.

For example, if number 5 is assigned to a variable of type Boolean, it will convert the 5 to a value of true and then store true in the variable.

Casting
In your code, you explicitly indicate you want the compiler to treat it as a different data type. To do this, you declare the data type as a function call with the value you wish to transform as the parameter.

For example,
var sEmergency:String = "911";
var nEmergency = Number(sEmergency);

On a related note, AS3.0 introduces a new operator called “as”. It work very much like type casting.

var zipCode:String = "90210";
var num:Number = zipCode as Number;
trace(num); // Output: 90210;

Source
Adobe Programming ActionScript 3.0

ABOUT THE AUTHOR
Mark Lapasa is an experienced (5yrs+) Flex/Flash/ActionScript Developer. He has product experience developing client-side application software for the Financial CRM and Online Gaming Industries.

3 Responses to “Type Conversions: Coercion vs. Casting”


  1. argo
    on Apr 8th, 2008
    @ 1:45 am

    Hi, I know this entry is from 2006, so maybe then the ‘as’ operator in AS3 acted a bit different. But I wanted to leave this comment, as the ‘as’ thingie just confused the hell out of me (I’m new at as3 though I know as2 really well).

    I was trying to figure out why using ‘myVar as Number’ always returned 0. I was trying the ‘as’ operator, as it’s something new for as3, and I saw it in a forum, just like you mentioned, that it could be used to cast an expression to another type or class. But it looks like that explanation is wrong. ‘as’ is used to compare whether an expression is of type ‘type’ (in your example ‘Number’). At least that’s what it says in the as3 help files:

    [as]
    Evaluates whether an expression specified by the first operand is a member of the data type specified by the second operand. If the first operand is a member of the data type, the result is the first operand. Otherwise, the result is the value null.


    However, it is interesting to see that even the docs are kind of wrong, as in your example, the trace outputs 0, not null. But if you do the following: trace(zipCode as Number) it outputs null.

    So, it looks like if you assign the whole expression to a var of type Number, the result is 0 instead of null. Kind of interesting, but damn awful.

    I guess it does that because var num:Number can’t be null. var num:Number=null returns a compiler error.


  2. mlapasa
    on May 4th, 2008
    @ 6:16 pm

    Interesting. I think AS3 back in July 2006 was still in Flex 2 Beta. So when I tested back then, it had worked. Thx for the update.


  3. jooher
    on Sep 20th, 2009
    @ 7:26 am

    var zipCode:String = “90210″;
    var num:Number = zipCode as Number;
    trace(”Num:”+num); // Output: Num:0 (NOT 90210 !!)

    This is exactly what is expected. What happens is:

    1. ‘as’ checks if zipCode is Number?
    2. zipCode is String, not Number, so ‘as’ returns null
    3. ‘num:Number = null’ coerces null to 0, as clearly stated in AS3 docs
    4. so, finally num is 0. all correct.

Leave a Reply

© 2009 knowledge.lapasa.net. All Rights Reserved.

This blog is powered by Wordpress and Magatheme by Bryan Helmig.