Macros 在生成宏中获取抽象基础类型?

Macros 在生成宏中获取抽象基础类型?,macros,haxe,Macros,Haxe,例如,我有一个抽象类型的Bool abstract ABool(Bool) from Bool to Bool {} 我有一个这样的字段 var abool:ABool; 我需要的是了解构建宏中的基本类型(Bool)。\if宏 #if macro import haxe.macro.Expr; import haxe.macro.*; #end class Builder { macro static public function build():Array<Field&g

例如,我有一个抽象类型的Bool

abstract ABool(Bool) from Bool to Bool {}
我有一个这样的字段

var abool:ABool;
我需要的是了解构建宏中的基本类型(Bool)。

\if宏
#if macro
import haxe.macro.Expr;
import haxe.macro.*;
#end

class Builder {
    macro static public function build():Array<Field> {
        var fields = Context.getBuildFields();
        for (f in fields) {
            switch (f.kind) {
                // looks at class variables and properties
                case FVar(t, e) | FProp(_, _, t, e):
                    // t is a haxe.macro.ComplexType,
                    // lets convert it to a haxe.macro.Type,
                    // such that we can check if it is an abstract type or not
                    var type = Context.typeof(macro {var a:$t; a;});
                    switch (type) {
                        case TAbstract(t, params):
                            var underlyingType = t.get().type;
                            trace(underlyingType); //TAbstract(Bool,[])
                        case _:
                            // ignore things that are not of abstract type
                    }
                case _:
                    // ignore methods
            }
        }
        return fields;
    }
}

abstract ABool(Bool) from Bool to Bool {}

#if !macro
@:build(Builder.build())
#end
class Test {
    var abool:ABool;

    static function main() {

    }
}
导入haxe.macro.Expr; 导入haxe.macro.*; #结束 类生成器{ 宏静态公共函数build():数组{ var fields=Context.getBuildFields(); for(字段中的f){ 开关(f.kind){ //查看类变量和属性 案例FVar(t,e)| FProp(u,u,t,e): //t是haxe.macro.ComplexType, //让我们将其转换为haxe.macro.Type, //这样我们就可以检查它是否是抽象类型 var type=Context.typeof(宏{var a:$t;a;}); 开关(类型){ 案例选项卡(t,参数): var underyingtype=t.get().type; trace(underlyngtype);//TAbstract(Bool,[])) 个案: //忽略不是抽象类型的东西 } 个案: //忽略方法 } } 返回字段; } } 从Bool到Bool{}的抽象ABool(Bool) #如果!宏 @:build(Builder.build()) #结束 课堂测试{ 变量abool:abool; 静态函数main(){ } }