Rorick
Rorick

Reputation: 8953

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?

Upvotes: 4

Views: 1003

Answers (6)

fenomas
fenomas

Reputation: 11139

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.

Upvotes: 2

Daniel Carvalho
Daniel Carvalho

Reputation: 557

Although the use of untyped variables is rare in my work, which is best, there are situations where it can be useful and significantly simplify problems. I've actually just asked a question where this untyped nature of variables shows up. Being able to dynamically add properties to an object provides great flexibility and doesn't force you to create a custom class, which takes longer to code and you might not ever use it again.

Secondly, I'm currently working on a project that dynamically instantiates classes using getDefinitionByName().

For example: Each section of a website I'm working on has its own class associated with it. Now, as far as I know, I could create a very redundant switch case that checks what section you are changing to, and then instantiate that class by using a variable typed for each section. OR, I could create a single variable "sectionClass", that dynamically becomes the appropriate section class every time a section changes.

That's a case where strong typing a variable such as sectionClass seems impossible (that I humbly know of) and why I like the option of untyped variables.

The catch is, even with getDefinitionByName() you need to declare a variable by type for each class anyways to include them into the Flash AVM (ActionScript Virtual Machine);P

Upvotes: 0

Allan
Allan

Reputation: 3314

I use them where I want someting generic. ActionScript does not allow you to create generics (although the new Vector type is pretty much a generic).

For example I was messing around and made a double linked list class that could hold any data type so the :* is perfect for that. However its not exactly the nicest thing since there is no type safety (unlike the Vector class).

Upvotes: 0

Ian Ringrose
Ian Ringrose

Reputation: 51897

ActionScript is an extension of a very old version of java-script. At one point everyone was developing their one versions of java-script. Only two main versions went into common use

  • Jscript / java-script by netscape was used by all web browers
  • ActionsScript is used in flash etc.

The “var” keyword is part of jscript, therefore ActionScript has it.

I think in ActionScript2 the var keyword was restricted to Objects as part of the attempt to give ActionScript a stronger (more static) type system. ActionScript3 tries to be more like ECMAScript, so that jscript programmers have less problems using it, therefore ActionScript3 defined “var” (and untyped variable) like jscript does.

There are a lot of very odd things in jscript, it is just not like any other language in main stream usage, due to history ActionScript has a lot of these odd thinks as well.

ActionScript is being pulled in two directions, one group of people want it to be just like jscript, another group of people want it to be the best language it can be in it’s own right. Meanwhile a lot of developers have to use it occasionally and get very confused.

(Hence some developers are looking at Silverlight just because it uses C# / VB.NET that they already know. History will tell us the outcome of this in 10 years time…)

See also

To sum up:

So I would says the “rationale of untyped variables in actionscript-3” is a combination of history and policies and have very little to do with good language design or computer science.

Upvotes: 0

Amarghosh
Amarghosh

Reputation: 59451

To add to what oggy said:

Earlier in the days of AS1 (and even AS2), the main users of the language were flash designers who didn't have a deep programming background. So not having to worry about typing/casting might have been helpful for them.

Untyped arrays, dynamic classes (like Object and Array) where you can add a new property to an object after instantiating it, are another examples of language constructs that regular (strongly-typed) programmers might find awkward.

The latest version of the language (AS3) retains some of these features (may be for backward compatibility and/or to continue being a simple language for designers).

Upvotes: 0

oggy
oggy

Reputation: 3571

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!

Upvotes: 3

Related Questions