I haven't really seen this spelled out for ActionScript, so your mileage
may vary because what comes next is from C# knowledge and AS3 experience.

There are two ways of type-casting: prefix and the *as* operator.

Prefix type-casting is what you are probably used to. It looks like this:
var x:ClassABC = new ClassABC();
var y:ClassDEF = ClassDEF(x);

In this example, if x can be casted to ClassDEF, it will be (in other
words, if the types are convertable due to polymorphism through extension
or interface implementation). If, on the other hand, the type cast cannot
succeed because ClassABC is not related to ClassDEF, then you will get
a *runtime
exception* on the order of "Type ClassABC cannot be converted to type
ClassDEF."

In essence, the *as* operator works like this:
expression *as* type
can be translated such that
expression *is* type ? type(expression) : null;

Working with the example above, the *as* operator works quite similarly.
var x:ClassABC = new ClassABC();
var y:ClassDEF = x *as* ClassDEF;

In this example, however, if the casting operation fails, then *no
exception* is thrown. This means you need to check y for null before
proceeding, but also that it is more or less type-safe. If the cast fails,
you just get null. In the case of non-nullable primitives like int, I think
you get the default (NaN in the case of int), but I am not sure of this for
AS3 (in C#, it is not allowed at compile-time).

My understanding has always been that *as* is faster and is therefore more
appropriate when casting inside a loop. It's also safer as, in my opinion,
the contents of your set may not be guaranteed (though perhaps more
guaranteed now that we have pseudo-generics with the Vector<> class). This
is a matter of opinion.

More appropriately, when do you use prefix casting and when do you use the
*as* operator?

   - Use prefix casting when the target *should* be of the destination
   class. In this case, a runtime exception is the appropriate response
   because execution will cease and no more incorrect assumptions will be
   executed upon.
   - Use the *as* operator when the target *might* be of the destination
   class (or, in my opinion/code, when you are casting inside a loop). Check
   for null, and handle accordingly.


I personally use *as* about 90+% of the time because if something should be
typed, it usually already is and I'm using it through an interface or base
class, but that's just me. I've worked with many people on both sides of
the aisle, and as long as you check for null or handle exceptions as
appropriate, you'll be all right!

Hope that helps. Happy New Year,
Michael


On Tue, Dec 31, 2013 at 12:15 PM, Greg Huddleston
<[email protected]>wrote:

> ouch!  you are making my head hurt thinking about PL1 / algol paradox et
> al.
>
> lol  happy new year to all  //GregH
>
> Sent from my iPhone
>
> On Dec 31, 2013, at 12:12 PM, "Davidson, Jerry"
> <[email protected]> wrote:
>
> > I started in PL/1 in the late 70s.  I'm always happy to find someone
> else that was working with it.  Didn't start PC stuff until Paradox.
> >
> >
> >
> > -----Original Message-----
> > From: Scott Matheson [mailto:[email protected]]
> > Sent: Tuesday, December 31, 2013 1:15 PM
> > To: <[email protected]>
> > Subject: New Years resolution, type casting
> >
> > Hi
> >     I have been hacking code for 30 years, I started with PL1 and moved
> on, I have been hacking Flex for the past 3 years getting along nicely,
> before Flex I never worked with an object based language
> >
> > My son 28 codes with me, for the past 2 years we have work on different
> areas of the same app
> >
> > So my New Years resolution is to understand the AS as I never use AS
> (type casting) , Chris makes use of the AS all the time, does anyone want
> to help me understand AS and make my resolution, or am I too old :(
> >
> > Scott
> >
> >
> >
> >
> > Sent from my iPad
> >
> > ________________________________
> >
> > Disclaimer: This electronic mail and any attachments are confidential
> and may be privileged. If you are not the intended recipient, please notify
> the sender immediately by replying to this email, and destroy all copies of
> this email and any attachments. Thank you.
>

Reply via email to