在 Haxe 宏中获取对象的类型
Get type of an object in a Haxe macro
我想在宏中获取一个对象的 class 以便我可以访问它的静态变量:
// 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());
您可以使用 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{}
结合使用生成完全限定路径。
我想在宏中获取一个对象的 class 以便我可以访问它的静态变量:
// 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());
您可以使用 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{}
结合使用生成完全限定路径。