我想在宏中获取一个对象的类,以便我可以访问它的静态变量:
// autoBuild macro adds static field "id_ : Int" to all subclasses
class Base {
}
class Child1 extends Base {
public function new() {}
}
class Child2 extends Base {
public function new() {}
}
class Container {
public function addChild(index: Int, object: Base) {}
macro function add(object: ???) {
// find out class of on object
// ???
// var id = class.id_;
this.addChild(id, object);
}
}
所需用法:
var c = new Container();
c.add(new Child1());
c.add(new Child2());
答案 0 :(得分:3)
您可以使用Context.typeof()
获取表达式的类型 - 然后您需要进行一些模式匹配以找出类型的名称。以下仅适用于类,因为它只匹配TInst
,但可以扩展:
import haxe.macro.Context;
import haxe.macro.Expr;
class Container {
// [...]
public macro function add(self:Expr, object:Expr):Expr {
var name = switch (Context.typeof(object)) {
case TInst(_.get() => t, _): t.name;
case _: throw "object type not found";
}
return macro $self.addChild($i{name}.id_, $object);
}
}
这将生成以下代码:
var c = new Container();
c.addChild(Child1.id_, new Child1());
c.addChild(Child2.id_, new Child2());
请注意,访问_id
通过它的不合格名称只有在实际导入(或顶级)时才是安全的 - 实际上您要使用t.pack
与$p{}
结合使用以生成完全限定的路径。