Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly does var x:* mean in actionscript?

Tags:

actionscript

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

like image 896
Simon Avatar asked Oct 16 '08 05:10

Simon


2 Answers

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.)

like image 82
fenomas Avatar answered Jan 03 '23 17:01

fenomas


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.

like image 28
paxdiablo Avatar answered Jan 03 '23 18:01

paxdiablo