Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the rationale of untyped variables in ActionScript 3?

I started to learn Flex and ActionScript and encountered interesting statement: untyped variable. That is:

var x:*;

or just

var x;

I found out that they can hold undefined values. Variables of Object type cannot. But I don't understand the purpose of them. I don't think that someone often need to distinguish undefined and null value - what is possible with these variables. Though it seemed to be equally possible in ActionScript 2 with no untyped variables. Variable x was treated like Object in statement var x; and Object variables could carry undefined value.

So what is the rationale of these "truly untyped" variables? Why are they introduced into the language?

like image 877
Rorick Avatar asked Sep 23 '09 09:09

Rorick


People also ask

What is a variable in ActionScript?

In ActionScript 3.0, variables are always assigned the scope of the function or class in which they are declared. A global variable is a variable that you define outside of any function or class definition. For example, the following code creates a global variable strGlobal by declaring it outside of any function.

What is an array in action script?

Actionscript Course Arrays are variables that store multiple values in a single variable name. So, an Array is a variable with an ordered list of values.


2 Answers

The plain answer to your question is that the specification of ActionScript3 was based on draft proposals for ECMAScript, 4th revision, and those drafts specified that data could be untyped. So, AS3 allows data to be untyped. As for the rationale, I'd say it's almost certainly some combination of allowing backwards compatibility with ECMA-3 code, and accessibility to programmers accustomed to revision 3's optional typing. But only the authors of the ECMA draft could answer that question, and it's somewhat orthogonal to what you're asking.

Anyway the answer to why ActionScript3 includes untyped variables is that the proposal it was based on included untyped variables. I have no idea where the other answers in here are coming from, particularly the ones implying that this feature is to comfort AS2 programmers or people not ready for a "real language". If Macromedia/Adobe had deviated from the ECMA proposals on any given feature, it would make sense to suppose they did so for such reasons, but the feature in question is implemented as per the proposals.

like image 89
fenomas Avatar answered Sep 22 '22 18:09

fenomas


ActionScript, like JavaScript, is based on the ECMAScript specification. In ECMAScript, all variables are untyped - it's a dynamically (and weakly) typed language. So as a matter of fact, the static type system of AS introduced in AS2 is a Macromedia addition - untyped variables have been there forever.

Now personally, I consider the benefits of adding a static type system somewhat dubious, but the undefined value is undoubltely one of the great f***-ups of ECMAScript. Accessing a nonexistent property of an Object doesn't raise an error, it simply returns an undefined value. However, attempting to access a property of an undefined value does raise an error - a great way to get your errors show up far from the place where you made them!

like image 21
oggy Avatar answered Sep 23 '22 18:09

oggy