Why doesn't ActionScript have "generics"? - actionscript-3

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.

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.

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.

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.

Related

Migrating from AS2 to AS3

I would like to know from someone who have already done that, any recommendations and things I have to take a special look, I have seen some articles related to the topic, googled it, etc...
but I would like the advice from stackoverflower x)
I already know object oriented programming in c++, using classes and etc, but I can't quite understand AS3 packages and stuff, but i'm very familiarized with AS2.
Thanks
Drop everything you know, start fresh. AS2 is different than AS3. Don't try to do the AS2 thing with AS3.
Read & Learn the Adobe LiveDocs
Learn how the display list works.
Learn AS3 coding standards, write clean readable code
Learn how to use common actionscript libraries, TweenLite, Gaia framework, RobotLegs, Temple Library, Pure MVC, Away3D, as3corelib etc.
Never ever code inside the Flash IDE actionspanel, there are really nice actionscript editors like FlashDevelop, FDT, FlashBuilder, IntelliJ.
My experience migrating from AS2 to AS3 has been pretty smooth, so smooth that I would never go back and squirm at the thought of maintaining old AS2 code.
Firstly I would get familiar with the display list, here is a good article.
Then I would gain an understanding of the new types in AS3, especially the difference between Number, int and uint as you don't need to lump everything in Number anymore.
Do some reading up on the event system and how you can capture events that have bubbled up from other objects, and how you can use capture and stop them propagating further, and how to avoid unnecessary clicks on nested objects in buttons you create.
Like you say you already know OOP, so I would then suggest limiting timeline code as much as possible, write everything in classes.
XML is handled using e4x which makes xml parsing trivial, you will find this a breeze to work with compared to AS2.
The drawing api is now contained in a graphics library accessible through many display objects.
Get to know a good framework, I highly recommend pure MVC AS3
Finally for animation - there can be only one library - Greensock TweenMax AS3 of course ; )
EDIT :
I have looked around for some resources that I think will be of interest to you, I based these choices on my experiences and what I believe are key areas of research:
Presentation on AS3 by Grant Skinner - Excellent overview that you can refer to time and time again.
Getting Started with ActionScript 3.0 - Comprehensive approach to using AS3 with Flash CS3.
Senoculars tips of the day - because you already code in AS2 you should understand a lot of these tips and how they differ in AS3.

Can I use the <> syntax in AS3 for anything other than Vector.<T>?

I've been learning a fair bit of C# lately and noticed that the <> syntax is used a lot, eg:
Content.Load<AssetType>("asset name");
The only place I've seen this used in AS3 is when using Vectors:
var enemies:Vector.<Enemy> = new Vector.<Enemy>();
Can I implement use of this syntax myself somehow in ActionScript 3? For example, I may want my own method similar to Content.Load().
The syntax you're referring to is called generics, and Vector is the only way they can currently be used in AS3.
Here is a link to a related question about AS3's generics, why you can't create your own.
Hope this helps!
The only other place where the angled braces are used (as far as I know) is for declaring inline XML:
var myXML:XML = <rootNode><dataNode>What's up?</dataNode></rootNode>;
Which is horrible programming practice. I don't believe there is any way to extend AS3 in the manner you describe, as it is only possible to create class extensions, not entirely new language syntax.
As far as I know, the Realaxy editor provides generics through a language extension. The dev talks about it here (see the comments)
The only problem is that it ties you to the editor.
That said, you could also probably fake it through reflection, or just doing your own runtime checking. Not a perfect solution though

what is so bad about not using classes while creating applications in flash?

Why would it be better to use classes while programming in ASE than just using object oriented programming? Can you give me some good real world sittuations of when you should use classes? I'm asking this question because I want to better understand why I should use classes in flash apps I'm going to make and am making.
If you're just using AS code on frames, you're not using object oriented programming per-se, you're just running procedural scripts affecting objects.
The advantages for flash are the same for any other class-based OOP paradigm:
Separation of code into understandable hierarchy
"easy" code reuse
All design patterns that are associated with OOP
inheritance and extension
(for more just google "advantages of Object Oriented Programming")
I find that the advantages of OOP don't seem to make a difference during the first iteration. You're writing the same essential code either way. The advantage really comes out in the second or third similar project where you can start to reuse stuff that you made before, tweak a few parameters, extend a few classes, and have a different looking flash program without rewriting all the code.
If you make enough projects, eventually you'll find yourself with a library or framework of classes that you can easily reuse to make very powerful applications.

Runtime Class creation in actionscript-why and for what purpose?

Hi
Recently in actionscript it has been made possible to create Classes at runtime. Thi seems quite cool, but I am perplexed thinking of a situation in which this might be useful. Anyone have any ideas?
First of all, the uses for this in ActionScript is limited. So start of understanding what it actually is, and how other languages use it.
See Self-modifying code on wiki:
http://en.m.wikipedia.org/wiki/Self-modifying_code
Also see Reflection:
http://en.m.wikipedia.org/wiki/Reflection_(computer_science)
As an example of how it might be useful, I'm currently working with genetic algorithms to modify code at runtime. This way I can test every permutation (varying initial values and methods) without having to create classes for them, with the added bonus of exporting a .swf only containing the winning permutation.

Actionscript 3.0, why is it missing good OOP elements?

Anyone who has programmed with actionscript 3.0 has most certainly noticed its lack of support for private constructors and abstract classes. There are ways to work around these flaws, like throwing errors from methods which should be abstract, but these work arounds are annoying and not very elegant. (Throwing an error from a method that should be abstract is a run-time check, not compile-time, which can lead to a lot of frustration).
I know actionscript 3.0 follows the current ECMAscript standard and this is the reason for its lack of private constructors, but what about abstract classes, are they not in the ECMAscript standard eather?
I guess the more specific question is why does ECMAscript standard not support private constructors? Is it something that can be look forward to in the future?
I have been wondering this for quit sometime, any insight will be much appreciated.
private constructors and abstract classes are not "good OOP elements". they're nice hacks originated in C++. in more dynamic languages they're usually not needed.
abstract classes in particular are totally unneeded, since you don't have to declare the interface in the ancestor to comply with an interface. in fact, you don't even have to inherit from a common ancestor to use some polymorphism.
i'm not saying that AS is better without something like that; rather that you should think in the language you're using, not trying to translate from whatever you're used to.
Neither private constructors nor abstract classes were defined in the old ECMAScript 4 standard on which ActionScript 3 was based. If I remember correctly, the ECMAScript Working Group chose not to implement these more complicated OOP features because there was a certain focus on simplicity and backwards compatibility with older versions of ECMAScript. I interpreted what I heard from them as "we may add these features later, but let's take it slowly". The abstract keyword, for example, is a reserved word, so they have this stuff in mind.
It's worth noting that the working group has chosen to restart the next version of the language with a new focus. This effort is called "Harmony" because two rival sub-groups had very different opinions about where ECMAScript should go in the future. It's a bit of a compromise. Harmony is going to progress much more slowly than the old ES4, and even the class syntax that was already implemented in AS3 will be dropped from the standard initially. In other words, they're going to keep it looking a lot more like today's JavaScript for a while to focus on some other features that are important to the group that was going to branch. This will become ES3.1. Later, classes and some of the more Java-like OOP features will be reconsidered for a new ES4.
What about AS3, though? Basically, Adobe jumped the gun by using a standard that wasn't yet complete, and they got bit by politics. However, Adobe intends to stay involved with the ECMAScript Working Group, and they will likely consider adding features that the working group recommends. That said, AS3 may never be a complete (or fully compatible) implementation of a future ECMAScript. What does that mean? Well, since they're non-standard again, Adobe has the option to add features to ActionScript, even if those features aren't part of a standard. If you feel abstract classes or private constructors are important to you as a developer, request these features in the public Flash Player bug database, or vote for existing feature requests, if they're already present.
I know, this question is really old, but I just stumbled accross and had to amend this:
Constructors are not OO. Objects should be instantiated by factories.
Abstract classes are not OO. Instead one should use interfaces (which AS3 has) and mixins (which AS3 doesn't have).
AS3 is a poor language, but for many more fundamental reasons than these two. AS3 basically is a structured representation of AVM2 bytecode. A 1:1 mapping is easily feasible. And I suppose, it's likely to stay like that, because Adobe appearently has more interest in driving forward platform features, their commercial authoring tools and the flex framework (including mxml).
greetz
back2dos