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

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

Related

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.

Actionscript 3: What's difference between interface and abstract and when to use them..?

I was wondering the differences between abstract and interface in actionscript 3 and when to use them..I have searched google but still couldn't understand them....I hope someone here can give me few tips...Thanks a lot!!!
The difference is that interface is valid actionscript, but abstract is not...
Now, in other languages you can mark a method or a class as abstract. This is somewhat like an interface, in that abstract means it has no implementation (for a method) or cannot be instantiated (for a class).
So, if a class is abstract, it means you cannot create an instance diretly (with new), but rather you have to extend the class to access its functionality.
An abstract method is pretty much like a method defined in an interface. When you extend a class that declares an abstract method, you have to provide an implementation that has the same signature, or your code won't compile. An abstract method makes the class abstract automatically, because otherwise you would be able to create an instance of an object that has an unimplemented method.
With an abstract class, you have some of the features of an interface (i.e. you define a method whose concrete implementation has to be provided) but you also can have other methods that are implemented and ready to use.
(This is a general explanation; maybe this is bit different in language X, but I think this gives you the basic idea)

Aspect Oriented Programming Library/Framework for Actionscript 3?

I'm looking for a full featured AOP Library for Actionscript 3.
The following projects I noticed so far, but they all seem to have their problems:
http://farmcode.org/page/Sodality.aspx
looks most promising so far, however it requires you to create a whole new class for every AOP "call" I believe, and it forces you to follow quite a lot of restrictions, anyone has experience with it?
http://code.google.com/p/loom-as3/
this one is discontinued
http://code.google.com/p/floxy/
dynamic proxy generation? this isn't really AOP as I know it, right?
http://code.google.com/p/flemit/
dynamic byte code generation? this is something AOP needs I think, but not the full featured AOP framework I am looking for
Does anyone know of a better solution? Or does anyone have any experiences with AOP in Actionscript 3?
Best regards,
Tom
AOP in ActionScript 3 is really hard. All aproaches have their problems:
proxying: actually works fine and transparent to the programmer, but not to the runtime. If any function signature expects an object of type A, then a proxy to an object of type A will fail to pass the typecheck
bytecode generation: well, sort of works, but not too good. loading bytecode in AVM2 is asynchronous, so you can't do it just in time. Also, once bytecode is loaded, there's no way of discarding it, so you can not modify a class a second time.
anything else is verbose
What you can do is to use Haxe. Not only does Haxe have other advantages over AS3, but it also allows some AOP. There are two approaches:
Dynamic Methods
In Haxe, you can declare methods to be dynamic. These can be replaced at runtime to provide advices. Let me explain, what happens under the hood. The following Haxe code:
public function foo(param1:Type1):Type2 { /*body*/ }
public dynamic function bar(param1:Type1):Type2 { /*body*/ }
Is the equivalent of the following AS3 code:
public function foo(param1:Type1):Type2 { /*body*/ }
public var bar:Function = function (param1:Type1):Type2 { /*body*/ }
Using the latter always performs worse than the former, since there's a lot of runtime type checking involved. In Haxe however, you do not lose the compile time benefits of strict typing, unlike in AS3.
Likewise, using Haxe's accessors (that are indeed very different from AS3's), you can also use AOP for properties:
public var foo(get_foo, set_foo):Type;
dynamic function get_foo() {//Haxe will automatically infer types: http://haxe.org/ref/type_infer
return this.foo;
}
dynamic function set_foo(param) {
return this.foo = param;
}
Any methods, that are dynamic can be replaced at runtime to do whatever you wish. AS2 allowed this, and libraries like as2lib provided AOP. unfortunately, it is no longer available. But I guess you can figure out the rest on you own.
Proxying
Haxe has the notion of anonymous types. At runtime, these are merely * (thus a perfomance loss is to be expected), but at compile time, they are type safe. What you can do, is create a proxy to an object of whatever type you need and use it as AOP container. In your whole code, you never use its type explicetely, but rather an equivalent anonymous type. The problem with proxying I described will be gone. The only thing you need to do is to make an unsafe cast of your proxy to that anonymous type.
Check out Mixing Loom, a foundation for AOP in Flex / ActionScript:
http://www.jamesward.com/2011/04/26/introducing-mixing-loom-runtime-actionscript-bytecode-modification

Actionscript 3 - Type.<T> syntax

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

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.