Better Casting Through ActionScript 3



In ActionScript 2, you could cast an object to a type by using:

ClassName(ObjectToCast)

This syntax is somewhat different than what you have in other languages. Usually in C-like languages you'll use something like this:

(ClassName)ObjectToCast

It doesn't seem a lot different, does it? But there is a problem with the way casting is done in ActionScript 2. ActionScript 2 also contains global creation functions for native data types-that look identical to typecasting syntax. The example I would continuously encounter was when I had a method that returned an object that I knew was an array. Let's look at an example. For the sake of argument say I have a method called getObject() that returns any object type-and I want to return an array from it. According to ActionScript 2 typecasting, I should be able to do something like this:

var MyObject:Array;
MyObject = Array(getObject());

But since global native data type functions take precedence, this doesn't actually cast the object from the getObject() method to an array-instead it creates a brand new array. You might be thinking this doesn't sound too bad-you're creating a new array of the returned array. Unfortunately, the new array you're creating isn't a replica of the array being returned from getObject()-its a new array with the array being returned from getObject() as the first element. Bleh! There is no good way to get around this in ActionScript 2-basically you're stuck.

In ActionScript 3, there is an easy way to solve this problem. ActionScript 3 does support the ActionScript 2 method of typecasting (with the slight difference that type exceptions are now thrown instead of the variable you're casting being set to null if a cast failure occurs) but it also introduces a new typecasting mechanism using the as keyword. You're basically saying "treat my object as this type".

To solve the problem related above, you can do this:

var MyObject:Array;
MyObject = getObject() as Array;


6 comments :

Eren Bali said...

I guess many as3 programmers like me are looking for something. How we can implement casting for our own classes. We can do this in c++, but i could not find any documents about this in as3.

I think As3 is still young and does not include advanced OO operations like overriding operations and casting but i am not totally sure yet.

HUWebDev said...

I might be misunderstanding, but you can cast an object to your own class simply by doing 'MyObjectType(MyObject)' or 'MyObject as MyObjectType' like in this post.

You can override inherited methods in AS3 by using the 'override' keyword in your method signature like this: 'override public function myOverriddenMethod() {}'.

I'm not sure if by 'overriding operations' you mean operator overloading-which AS3 doesn't support, but this is an OOP feature I've rarely had need for(your mileage may vary).

Anonymous said...

there a many things, that as3 is lacking of. that casting-thing may be better, but far away from "ok" or "good".
as eren said, overloading functions is still not possible. you cannot define a second constructor that behaves like a converter. of course you can use this casting syntax, but to be honest, it wont help you for complex conversions.
overloading in general is a MUST, but not for adobe as it seems.

operator overloading is another story. it would be cool, but as huwbdev said, you'll rarely need it.

as3 is really messy when it comes to OO. i mean, all these statements like "override", "final", "dynamic" .. wtf? who needs that? eliminating the possibilty of extending classes , hooray!

you can "implement" two or more interfaces, but "extending" two or more classes into one on the other hand doesnt work.

if it wouldnt have so much more features than as2, i would shoot it straight to the moon.

Anonymous said...

Overloaded functions are simply a way of calling the same function name with a different number of parameters that may or may not have different parameter types. Although Actionscript does not support overloading in the classical sense; it does allow you to accomplish the above. Check out http://flexblog.faratasystems.com/?p=100

Anonymous said...

I would personally love operator overloading. The simplest example that comes to mind is currency classes and complex numbers. The Number class in flash is floating point which is a big no-no when working with anything like money. Without operator overloading you have to resort to code like this;

c.equals( a.add( b ) );

instead of;

c = a + b;

I'm not saying you'll write code with overloaded operators on a daily basis, but if you create and use libraries I'm sure you'll use the results fairly often.

Brad Gronek said...

I agree that AS3 is an improvement; however, the competition is Silverlight and .NET. Therefore, anything that is not fully oo will be a problem. Actually, I would embed a Java JVM inside the Flash executable (or create a java Flash runtime). It's probably too late to go down this path strategically, so we'll wait for ECMAScript and AS3 to play catch up and hope it matures quickly.