views:

187

answers:

6

Hi, All,

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 purport of them. I don't think that someone often need to distinguish undefined and null value - that 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?

A: 

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.

Ian Ringrose
Sorry? Question is why `var x;` is treated as untyped in ActionScript 3 instead of being Object as it was in ActionScript 2.
Rorick
Sorry, Ian, but I don't care about `var` keyword. I care about untyped variables. They appeared just in ActionScript 3 - three yers ago as of wikipedia. So it is relatively new odd stuff, but not old odd legacy.
Rorick
@Rorick, ActionScript 3 tied to keep to the spec of ECMAScript. EDMAScript is based on the legacy of jscript.
Ian Ringrose
Summarizing answers, it seems that untyped variables are just to comply with ECMAScript spec. Well, that can be the case assuming that ActionScript is statically typed while ECMAScript is not..
Rorick
+1  A: 

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!

oggy
Sure, but the only reason you don't get an error there is because Object is a dynamic class. If you're passing around plain Objects and accessing properties on them, you're essentially asking the VM not to do any error-checking.
fenomas
@fenomas: that's not necessarily how other dynamic languages work. In e.g. Python you can always set a new property on an object, but attempting to access a non-existing one raises an error.
oggy
Aye, it's not how Python works, but it's how AS3 works. What I'm saying is that the point you raise doesn't normally impact development, because one does not normally pass plain objects around (unless one is hacking).
fenomas
And what I was saying that the original ECMAScript behaviour (which still exists in Javascript) sucked. So I guess this was Adobe's attempt at rectifying it.
oggy
Not unless Adobe wrote the ECMA revision 4 spec, as I explain in my answer.
fenomas
A: 

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

Amarghosh
AS1, AS2, and AS3 were all based on ECMA specs. The idea that the typing was kept simple so designers could understand it doesn't make much sense unless the same was true of JavaScript?
fenomas
AS3 is more solid than its previous versions and it made the typing stricter. I've seen designer blogs discussing how AS3 is more 'programmy' and inconvenient for them, while I personally like it this way (plus method overloading and typed arrays). I was suggesting that Adobe might have had this in mind too when they designed AS3. http://jjeffryes.blogspot.com/2007/09/actionscript-30-vs-designers.html
Amarghosh
A: 

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

Allan
Isn't `:Object` suitable for this purpose? Why is `:*` better?
Rorick
yeah that would work to. According to Adobe:"An untyped variable is not the same as a variable of type Object. The key difference is that untyped variables can hold the special value undefined , while a variable of type Object cannot hold that value."
Allan
^ link:http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f9f.html
Allan
A: 

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

Daniel Carvalho
Why not `Object`? Is there something that makes it not fit?
Rorick
I do use Object for generic things that allows me to add properties as mentioned above. For the section thing, technically, you can, unless your Class extends MovieClip or something else which will throw out an error. I find it un-useful typing dynamic operations something as generic as Object. In my opinion, it might as well be untyped as typing it object doesn't really help clarify what specifically the variable is. Leaving it untyped, for me, gives me a better idea of what's going on.Strong typing something like that, is simply adherence to strict protocol with no value.
Daniel Carvalho
+1  A: 

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.

fenomas