Actionscript 3 - Type.<T> syntax - actionscript-3

Is it possible to use the Type.<T> syntax with user defined classes?
For example, let's say I want to create a List class, but I want the user to use it like this:
var myList:List.<String> = new List.<String>;
or is this syntax exclusively evailable to the Vector class only?
Thanks!

At the moment it can only be used by the Vector class. Any other attempted use of the syntax will throw a compiler error.

Have a look at Haxe ... you can compile Haxe to swfs as well, and unlike AS3, it supports generics ...
edit: Haxe can target flash10 ... unlike ActionScript 3, Haxe even can use alchemy opcodes: http://haxe.org/api/flash9/memory ... plus the generated bytecode is faster ... actually Haxe is a much better tool to leverage the potentials of the AVM2

Related

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

Is it possible to write an AS3 library with Haxe that uses type parameters?

First a little background: I'm looking for a way to create a "collection" library that abstracts the Flash Player version based implementation (Vector on FP10, Array on FP9) away from the calling code. I've already written a small AS3 lib doing that but...
...the performance is bad (especially because of two levels of indirection and the runtime type checks on the Array implementation)
...the code is ugly (since Vector types need to be defined at compiletime I needed a factory returning concrete Vector instances based on an Enum that contains only the supported types)
I'm currently looking into Haxe as a possible solution since it supports type parameters and is able to compile to various Flash Player versions (and apparently compiles into mmore optimized bytecode).
Now, my question is: Is there a way to write a library in Haxe that can be used like this in AS3 code
var foo:IMyInterface = new MyImplementation(int);
var bar:IMyInterface = new MyImplementation(getDefinitionByName("my.package.MyClass"));
with IMyInterface exposing the required methods (push, pop, ...)?
The basic idea is that I want to provide the type information at runtime and get a typesafe Flash Player version independent "collection" for use in the calling code without having to bother with conditional compilation fragments all over the place.
Can Haxe do something like that and if yes, how can I make it work?
There's an opportunity in Haxe to override native classes (e.g. int) in Haxe. take a look at the Metadata manual. Metadata has been added in version 2.06.
As for analogue of getDefinitionByName() method. Take a look at resolveClass() method of the Type class.

Disable / Enable Functions via Compiler Argument

). You know, in C++, C# and other educated languages, we got the chance to enable/disable the availability of some code in a certain class via compiler arguments. Take #if and #endif for example. You give an argument to the compiler, for example PROJECT_TDD_MODE, when a certain bunch of function would exist in the final code, but without defining that, or defining it as FALSE, the code would not exist in the compiled result.
Why don't we have something like this in AS3? ::- (
Conditional compilation is available in Actionscript too. Take a look at http://livedocs.adobe.com/flex/3/html/help.html?content=compilers_21.html

How would you implement generics like Vector.<T>?

So, ActionScript 3 has generics with the Vector class (Vector.). I have not seen any other class that uses generics like this.
If you were to implement Vector. yourself, how would you do it?
If you are after generics then you are best off to look at Haxe. It is very similar to ActionScript 3 and compiles to a swc or swf.
Coming back to haxe post above... you can create generics in haxe and then compile the code to as3. Not tried it recently to see how it is handled, but.. http://haxe.org/doc/flash/as3gen
Well you could make a class that takes a type T as a parameter, something like this
Foo( type:* )
And then in all the methods from which you can add items to the Vector class, I wouls raise an excpetion if the type of the object passed to the method doesn't match the type I defined when I instanciated my vector class.
That being said, you probably wouldn't get very good performance from a class like that, since everytime you handle an object you need to check it's real type at runtime, where as in C++ template classes are created a compile time, so no runtime type checking needs to be done.
AS3 does not support generic class declaration. There's a good discussion here

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