I'm trying to make some dark magic with macros in Haxe, I have a class named Entity
and I want to add a pool with the static
and private
modifiers:
Pool.hx
:
package exp;
class Pool<T> {
public function new(clazz:Class<T>) {
}
}
Entity.hx
:
package exp;
@:build(exp.PoolBuilder.build())
class Entity {
public function new(){}
}
PoolBuilder.hx
:
package exp;
import haxe.macro.Context;
import haxe.macro.Expr;
import haxe.macro.Type;
class PoolBuilder {
static public macro function build() : Array<Field> {
var fields = Context.getBuildFields();
var clazz = Context.getLocalClass();
var typePath = { name:"Pool", pack:["exp"], params: [TPType(TPath({name: "Entity", pack: ["exp"]}))] }
var pool = macro new $typePath(/* clazz? */);
fields.push({
name: "_pool",
access: [APrivate, AStatic],
pos: Context.currentPos(),
kind: FVar(macro: exp.Pool, pool)
});
return fields;
}
}
I have a problem with the typePath
params, and passing a Class<T>
as an argument to the constructor. The compiler display this error:
exp/Entity.hx:3: characters 1-7 : Invalid number of type parameters for exp.Pool
exp/Entity.hx:4: lines 4-6 : Defined in this class
Does anybody know how to solve it?