You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@flex.apache.org by Scott Matheson <sm...@intralinks.com> on 2013/12/31 20:15:29 UTC

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.

Re: New Years resolution, type casting

Posted by Joseph Balderson <ne...@joeflash.ca>.
Been an actionscript developer for about 13 years now, have watched the language
grow from a pseudo-scripting language (Flash 4) to an OOP scripting language
(AS1 in Flash 5) to a compile-time typed language (AS2 in w. Flash MX 2004, also
when Macromedia Flex 1 came out), to a proper strongly-typed language with AS3
in 2005, which is when Adobe Flex 2 came out. Moock's "Essential ActionScript
3.0" by O'Reilly was like, The Bible. (Those were the days, eh? :) Still is,
actually. So if you don't have a copy, GET ONE. AND READ IT COVER TO COVER. Do
that and you will "get" AS3 like no one else.

Had a smattering of other lang xp back in high school, did some C#, Pascal and
assembly back in the day. Went to art school, became an artist, then a graphic
designer, then a "web designer" (GOD I hated JavaScript and the netscape vs IE
browser wars -- the names change but the browser wars remain, eh?), then a
"deseloper" when I discovered Flash in 1999, then got back into programming
fulltime around 2003, never looked back since. Piddled around with Java, C, PHP
since then. But enough about me.

>From what I can remember of my basic AS3, typing goes like this:

Declared Type:
var someVar:String = "blah";

Unassigned Type:
var someVar:* = returnedFromService;
(You might want to do this when you're unsure what type you'll get back so that
your code does not throw a runtime error. Use it sparingly, or you'll have a
hard time tracking down bugs... oh gee! Just like JavaScript! ;) )

Untyped:
var someVar = "blah";
(Note this will generate a warning in normal compiler mode, but an error in
strict mode)

Explicit Type Conversion/Casting:
var quantity:int = int(quantityString);

Implicit Type Conversion:
var quantityString:String = "the text is" + quantity;

Type Checking (is operator):
var mySprite:Sprite = new Sprite();
trace(mySprite is Sprite); // true

Type Checking returning an expression (as operator):
var mySprite:Sprite = new Sprite();
trace(mySprite as Sprite); // [object Sprite]

Using explicit casting vs "as" operator:
var thisClip:MovieClip = MovieClip(myMC); // throws an error when it fails
var thisClip:MovieClip = myMC as MovieClip; // returns null when it fails

So depending on the circumstances, whether you want something to return a
runtime error or fail silently, you might want to use either explicit casting or
the "as" operator.

_______________________________________________________________________

Joseph Balderson, Flex & Flash Platform Developer :: http://joeflash.ca
Author, Professional Flex 3 :: http://tinyurl.com/proflex3book

Scott Matheson wrote:
> 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.
> 

Re: New Years resolution, type casting

Posted by Scott Matheson <sm...@intralinks.com>.
Michael
             It has just gone 12 in the UK and with your help I have completed my new year resolution in record time, all problems come down to simple concepts I will try and use more "AS" in 2014, saying that the old style still works, I will tell my son that's call experience :)

Have a good New Year when it comes your way

Scott





Sent from my iPad

> On 31 Dec 2013, at 23:28, "Michael Baird" <mb...@kairyt.com> wrote:
>
> 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
> <gh...@reachips.com>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"
>> <Je...@Illinois.gov> 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:smatheson@intralinks.com]
>>> Sent: Tuesday, December 31, 2013 1:15 PM
>>> To: <us...@flex.apache.org>
>>> 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.
>>

________________________________

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.

Re: New Years resolution, type casting

Posted by Michael Baird <mb...@kairyt.com>.
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
<gh...@reachips.com>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"
> <Je...@Illinois.gov> 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:smatheson@intralinks.com]
> > Sent: Tuesday, December 31, 2013 1:15 PM
> > To: <us...@flex.apache.org>
> > 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.
>

Re: New Years resolution, type casting

Posted by Greg Huddleston <gh...@reachips.com>.
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" <Je...@Illinois.gov> 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:smatheson@intralinks.com] 
> Sent: Tuesday, December 31, 2013 1:15 PM
> To: <us...@flex.apache.org>
> 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.

RE: New Years resolution, type casting

Posted by "Davidson, Jerry" <Je...@Illinois.gov>.
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:smatheson@intralinks.com] 
Sent: Tuesday, December 31, 2013 1:15 PM
To: <us...@flex.apache.org>
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.