Friday, May 6, 2011

Why doesn't ActionScript have "generics"?

Can anyone tell me why ActionScript 3, a statically typed language, doesn't have generics? Is it too much work? A historical thing? Is there some way to "fake" it that I haven't picked up yet?

Edit: thanks a lot for the answers! The Vector class is basically what I was looking for, and the other information was helpful too.

From stackoverflow
  • Let's first get proper containers and algorithms in actionscript and then worry about generics...

    as3 is not very different from javascript, btw, so your question would kind of apply to JS as well.

    David Wolever : mmm not so much. When used *without* typing and class notation, ActionScript /is/ very similar to JavaScript... But when you start using packages, class syntax and static typing, the way you use the two languages becomes fairly different.
    Kekoa : Well, that's like saying C++, or better, Objective-C, is "not very different" from C. In fact although C++/Obj-C is a superset of C, it's not really accurate to say that they are basically the same thing.
  • I believe it's a historical thing. ActionScript is based on ECMAScript (JavaScript is also based on ECMAScript). ECMAScript is a dynamically typed language, meaning that variables don't have their type declared. Generics are more useful in statically typed languages, wherein the type of variable is declared upfront. In a statically typed language, without generics you're stuck casting all the time from the root object (for example, Object in Java). This is not a problem in ECMAScript, because you can put anything you want into any data structure.

    So why didn't ActionScript add generics when they added static typing to ECMAScript? I can't be sure of that, but I think the premise of your question is a bit off - there are generic-esque containers, such as Vector. I might think they'd keep the dynamically-typed containers of ECMAScript (objects and arrays) for backwards-compatibility, but they already broke that between AS2 and AS3, so I'm not sure.

  • Parameteric types ( the word 'generics' is usually used in ECMAScript for generic methods, rather than the combination of parametric types and runtime polymorphism used in Java ) were proposed as part of ES4, but ES4 fractured and much of the type system proposed for ES ( including the parts implemented in ActionScript ) are not going into the next version. I can't say whether or not Adobe would want to go that way by themselves.

  • The new Vector class is a form of generics that Actionscript 3 now supports when compiled for Flash Player 10. They don't support the specification of your own generic classes, yet.

    I think Adobe will implement the ES4 standard eventually. It would be nice if they had a competitor who could push them quicker in the right direction. I was expecting a little more from the updates to AS3 when they moved to CS4, but I suppose the revolutionary Vector class will have to suffice.

    It looks like they spent a lot of time beefing up the libraries for Flex and AIR, so maybe they'll go back to improving the language support later, but it probably isn't a real priority. Remember, Adobe is in it for the money, not for the feel good of making the sweetest possible language.

    fenomas : ...huh? Firstly there's no such thing as "the ES4 standard", and there probably never will be since Ecma abandoned it. Secondly, Adobe *did* implement the draft of the ES4 standard, and they're pretty much the only ones who did. (They wrote a VM for it and donated it to the Mozilla foundation, before Ecma dropped it.) -->
    Kekoa : @fenomas Feel free to edit the post to make it more correct!


Post a Comment