tags:

views:

468

answers:

4

Its a little tricky to search for 'var:*' because most search engines wont find it.

I'm not clear exactly what var:* means, compared to say var:Object

I thought it would let me set arbitrary properties on an object like :

var x:*  = myObject;
x.nonExistantProperty = "123";

but this gives me an error :

Property nonExistantProperty not found on x

What does * mean exactly?

Edit: I fixed the original var:* to the correct var x:*. Lost my internet connection

+1  A: 

It's the "untyped" type. It just means that the variable can be of any type. Basically the same effect as using this:

var x = myObject;
Gerald
+1  A: 

It means that the type is not specified and can be used with any type. However, you can't set random properties on it. It will behave like whatever type you set it to. The exact syntax is:

var x:*;
AdamC
+1  A: 

It's a way of specifying an untyped variable so that you can basically assign any type to it. The code

var x:* = oneTypeObject;

creates the variable x then assigns the oneTypeObject variable to it. You can assign an entirely different type to it as well as follows:

var x:* = anotherTypeObject;

However, you still can't arbitrarily set or access properties; they have to exist in the underlying type (of either oneTypeObject or anotherTypeObject).

Both types may have identically named properties which means you can access or set that property in x without having to concern yourself with the underlying type.

paxdiablo
+2  A: 

Expanding on the other answers, declaring something with type asterisk is exactly the same as leaving it untyped.

var x:* = {};
var y = {}; // equivalent

However, the question of whether you are allowed to assign non-existant properties to objects has nothing to do with the type of the reference, and is determined by whether or not the object is an instance of a dynamic class.

For example, since Object is dynamic and String is not:

var o:Object = {};
o.foo = 1; // fine
var a:* = o;
a.bar = 1; // again, fine

var s:String = "";
s.foo = 1; // compile-time error
var b:* = s;
b.bar = 1; // run-time error

Note how you can always assign new properties to the object, regardless of what kind of reference you use. Likewise, you can never assign new properties to the String, but if you use a typed reference then this will be caught by the compiler, and with an untyped reference the compiler doesn't know whether b is dynamic or not, so the error occurs at runtime.

Incidentally, doc reference on type-asterisk can be found here:

http://livedocs.adobe.com/labs/air/1/aslr/specialTypes.html#*

(The markup engine refuses to linkify that, because of the asterisk.)

fenomas