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

1

6

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?

Quintal answered 23/5, 2011 at 12:30 Comment(0)
H
3

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.

Harhay answered 23/5, 2011 at 12:53 Comment(2)
Replacing the build in Array class with a custom one... Nice idea! I'd never have thought of that. :) Thanks for the hint, I think this is going to help me quite a lot.Quintal
@Baelnorn,always glad to help =) I advice to go on haxe.org and subscribe on their newsletter. You may ask any specific question about haXe there.Harhay

© 2022 - 2024 — McMap. All rights reserved.