Author Topic: What is the difference between these two casts?  (Read 1613 times)

test84

  • Key Contributor
  • *****
  • Posts: 1328
  • Karma: +0/-0
  • ت
    • View Profile
    • My personal site.
Hi,

What is the difference between:
Code: [Select]
Class(obj)and
Code: [Select]
obj as Class
-tnx
blog, twitter, Check out my award winning game, Rot Gut:

auriplane

  • Snails!!
  • Contributor
  • ****
  • Posts: 497
  • Karma: +1/-0
  • Snails!!
    • View Profile
Re: What is the difference between these two casts?
« Reply #1 on: Wed, Dec 21, 2011 »
If it fails, the former throws an exception, while the latter returns a null.

(I believe so, at any rate.)

pixelomatic

  • Active Member
  • ***
  • Posts: 131
  • Karma: +0/-0
    • View Profile
Re: What is the difference between these two casts?
« Reply #2 on: Thu, Dec 22, 2011 »
I redirect you to as3 documentation:
http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f87.html

I use the paranthesis casting with primitive data types and "as" casting on the rest. Maybe paranthesis casting only works with primitive values?

auriplane

  • Snails!!
  • Contributor
  • ****
  • Posts: 497
  • Karma: +1/-0
  • Snails!!
    • View Profile
Re: What is the difference between these two casts?
« Reply #3 on: Thu, Dec 22, 2011 »
I redirect you to as3 documentation:
http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f87.html

There's a page relevant to this topic which explains the as operator.  That page should be relevant, but doesn't appear to cover conversions to other than primitive types, which is why I decided not to link to the docs and add a disclaimer.  (Not a big fan of Adobe's docs!)

Quote
I use the paranthesis casting with primitive data types and "as" casting on the rest.

I notice you didn't say why.  But the reasoning shouldn't be the following:

Quote
Maybe paranthesis casting only works with primitive values?

Try it!

test84

  • Key Contributor
  • *****
  • Posts: 1328
  • Karma: +0/-0
  • ت
    • View Profile
    • My personal site.
Re: What is the difference between these two casts?
« Reply #4 on: Sun, Dec 25, 2011 »
If it fails, the former throws an exception, while the latter returns a null.

(I believe so, at any rate.)

So what would be different cases to use each of them? In other words,  why we have both of them to achieve conversion?
blog, twitter, Check out my award winning game, Rot Gut:

IQAndreas

  • Member
  • **
  • Posts: 35
  • Karma: +0/-0
    • View Profile
    • IQAndreas.com
Re: What is the difference between these two casts?
« Reply #5 on: Mon, Dec 26, 2011 »
So what would be different cases to use each of them? In other words,  why we have both of them to achieve conversion?
If the object you are type casting really is of that class, there is no difference.

But if it does not convert, Auriplane is entirely correct on the results. I quote Krilnon from the Kirupa forums:
Quote
The advantage would be that one does what you might want it to do in some situations and the other does what you might want it to do in others. For example, sometimes it's okay that a null value passes unnoticed through a part of your application. Sometimes it's not okay, so you might want an error to be thrown with a syntax that is about as concise as the syntax with the other behavior.

http://www.kirupa.com/forum/showthread.php?355957-Difference-between-ClassName()-and-as-ClassName&s=eaf45dd9cd10369a7017af7e00473eca
http://www.kirupa.com/forum/showthread.php?321242-typacasting&s=91295dd2ce9395baadbdc509bc0f76cb


So sometimes you want to throw an error, sometimes you just want to know that it didn't work to convert it.
Code: [Select]
function doSomething(target:DisplayObject)
{
    // Target NEEDS to be a Sprite. Throw an error if it's not.
    var container:Sprite = Sprite(target);
}

function doSomethingElse(target:DisplayObject)
{
    // Target "can" be a Sprite. If not, that's okay too.
    var container:Sprite = target as Sprite;
    if (container == null)
        { container = defaultContainer; }
}

pixelomatic

  • Active Member
  • ***
  • Posts: 131
  • Karma: +0/-0
    • View Profile
Re: What is the difference between these two casts?
« Reply #6 on: Mon, Dec 26, 2011 »
You can always check an object if it's instance of a type like this.

Code: [Select]
var foo:Sprite;
if (bar is Sprite)
   foo = bar as Sprite;
else
   // do what you want else here.

test84

  • Key Contributor
  • *****
  • Posts: 1328
  • Karma: +0/-0
  • ت
    • View Profile
    • My personal site.
Re: What is the difference between these two casts?
« Reply #7 on: Tue, Jan 10, 2012 »
Thanks guys for the explanation.
blog, twitter, Check out my award winning game, Rot Gut: