Tuesday, June 03, 2008

ActionScript: The difference between Object and * (an asterisk)

If you've ever wondered what the difference is between the following two statements, you aren't alone.

var result:Object = sendRequest();
var result:* = sendRequest();

It's fairly hard to Google for the explanation, but Subhash Chandra Gupta recently pointed me to a good example.

The full article can be found in the Flex 3 Help.

The difference is that * (an asterisk) signifies that the object can be any type and typing something as an Object will require you to cast if you are compiling in strict mode. Here's a greatly simplified example to demonstrate.

function returnOne():Object {
return 1;
}
var one:Number = returnOne(); // compiler error in strict mode

function returnTwo():* {
return 2;
}
var two:Number = returnTwo(); // no compiler error in strict mode

1 comment:

  1. So, it's like the ? wildcard matcher in Java Generics.

    ReplyDelete

Note: Only a member of this blog may post a comment.