"...but you can't actually type the return variable to anything except
Object or nothing."

Not true. In your example, you simply need to cast to the correct type,
i.e. String:
 
    var s:String = go() as String;

I don't think using * to get around compile-time type checking is a good
idea. You really should be casting values when assigning them from
generic types as a best practice.

Pete



________________________________

From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of Derek Vadneau
Sent: Wednesday, May 23, 2007 11:11 AM
To: [email protected]
Subject: Re: [flexcoders] star (*) data type vs. Object



"* is just like Object"

I couldn't disagree more.

I really dislike that Object is referred to in the docs about *. There
is an implication there that they are similar when they really aren't. 

This is a sore spot for me everytime I use Array.pop() and Array.shift()
in the Flash IDE. Both of those functions have their returns typed as
Object. Why? Someone thought it was a good idea because you can have any
type returned ... Well, no. Yes, you can have any type returned at
runtime, but you can't actually type the return variable to anything
except Object or nothing. 

For example, the Array.as file in the Flash 8 Classes folder defines:
function shift():Object;

If you try to do this you will get a compiler error:

var arr:Array = new Array(3); 
var n:Number = arr.shift(); << compiler error


The same holds true in AS3. For example:

function go():Object
{
return 'something';
}

Everything's valid here. Setting the return type to Object allows me to
return any type that subclasses Object, which is everything. 

var s:String = go(); << compiler error

"Implicit coercion of a value with static type Object to a possibly
unrelated type String."

The compiler doesn't swing that way!

So Object and * are NOT interchangeable. And the difference isn't
subtle. 

Use Object when you want to actually use generic objects and * when you
want to dynamically-type something.




On 5/18/07, Peter Farland < [EMAIL PROTECTED]
<mailto:[EMAIL PROTECTED]> > wrote: 

        See:
        
        http://livedocs.adobe.com/flex/2/langref/specialTypes.html#*
<http://livedocs.adobe.com/flex/2/langref/specialTypes.html#*> 
        
        The type * is just like Object but it can also store values that
are
        undefined. Object can only store null.
        
        It's useful to determine whether a dynamic property actually
exists on a
        type and just happens to be null, or whether it literally is not
defined
        on that type.
        
        ________________________________
        
        From: [email protected]
<mailto:flexcoders%40yahoogroups.com>  [mailto:
[email protected] <mailto:flexcoders%40yahoogroups.com> ] On
        Behalf Of Adam Pasztory
        Sent: Friday, May 18, 2007 2:42 PM
        To: flexcoders
        Subject: [flexcoders] star (*) data type vs. Object
        
        Can anyone tell me what the difference is between setting a
generic
        variable's data type to Object and setting it to *. Are they
        equivalent? 
        
        I tried to search for the answer, but it's hard to do a search
for *. :)
        
        thanks,
        Adam
        
        

        


-- 

Derek Vadneau 

 

Reply via email to