23

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.

David Wolever
  • 148,955
  • 89
  • 346
  • 502
  • 1
    I saw `Vector` and got excited that ActionScript had general-purpose generics that I somehow didn't know about yet. *Sigh*. I should have known better than to get my hopes up. – Jeremy Mar 10 '13 at 03:09
  • You can try Haxe. It have generics and other cool features. And it in-the-box compiling to Flash. haxe.org & try.haxe.org – Alex Koz. Mar 11 '15 at 23:41

4 Answers4

23

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.

James McMahon
  • 48,506
  • 64
  • 207
  • 283
Kekoa
  • 27,892
  • 14
  • 72
  • 91
  • 3
    ...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.) --> http://en.wikipedia.org/wiki/Ecmascript – fenomas Jan 18 '10 at 01:35
  • now that google dash is scaring them a bit they finally started talking about improving the language features with pretty much everything we've been asking for years on end - lookup "Actionscript Next". – JTtheGeek Mar 01 '12 at 19:13
  • 1
    Perhaps it's pertinent to note that the ActionScript NEXT or ActionScript 4 project was officially discontinued by Adobe. They open-sourced the specification drafts: https://github.com/adobe-research/ActionScript4 -- basically this means that Adobe has no plans to invest in language updates to actionscript. – Jeff Ward Feb 06 '14 at 18:07
2

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.

Dan Lew
  • 85,990
  • 32
  • 182
  • 176
2

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.

Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
0

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.

Assaf Lavie
  • 73,079
  • 34
  • 148
  • 203
  • 1
    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. – David Wolever May 11 '09 at 15:48
  • 8
    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. – Kekoa May 11 '09 at 15:59