Haxe Forum > Templates like C++ / Java ?
-
raul Aug 01 at 16:42
Is it possible to write code like:
class BaseClass { ... } class AClass { ... public <T extends BaseClass> T functionName(Class<T> type) { return type.cast(systems.get(type)); } ... }I did not found a way looking on documentation. Thanks.
-
Not sure what you mean exactly but the syntax is far from haxe.
Stuff like...
public function fred<T>( t: T ){ }
is possible but generics are complex to get right especially if you trying to only use them in a method, probably easier to consider Macros.
-
raul Aug 02 at 08:55
Syntax is for Java.
I just want to ensure that compiler allows this:
class Derived extends BaseClass{ public function on_derived() : Void { trace('Go'); } } ... AClassInstance = new AClass(); ... AClassInstance.functionName(Derived).on_derived(); // Derived extends BaseClass, so Ok ...
But also should forbid this:class Derived2 extends OtherClassNotDerivedFromBaseClass { public function other_function () : Void { trace('Other trace'); } } ... AClassInstance.functionName(Derived2).other_function(); // This should throw a compiler error, Derived2 does not extends BaseClass ...Any way to achieve this?
-
AClassInstance.functionName(Derived)
Sorry this is not valid haXe syntax and makes no sense to me. -
Raul,
This forum is for simple beginners questions.
It's best to ask your question on the mailing list, where all the people are, and where you will get a proper answer.
https://groups.google.com/forum/?hl=en#!forum/haxelangCheers,
Jan
PS: That is valid syntax btw.
-
Liburn Aug 13 at 22:13
This is working for Haxe 2.10 prior latest SVN changes:
class BaseClass { public function on_derived() : Void { trace('Base'); } } class Derived extends BaseClass { public override function on_derived() : Void { trace('Derived'); } } class AClass { public function new() { } public function functionName<T:BaseClass>(type:Class<T>):T { return std.Type.createEmptyInstance(type); } public function static_functionName<T:BaseClass>(type):T { return type; } } class Derived2 extends AClass { public function other_function () : Void { trace('Other trace'); } }
var AClassInstance = new AClass(); AClassInstance.functionName(Derived).on_derived(); // OK AClassInstance.functionName(Derived2).other_function(); // compiler ERROR