Macros 通用生成添加字段

Macros 通用生成添加字段,macros,haxe,Macros,Haxe,考虑以下代码: 类测试 { 公共静态函数main() { var foo=new FluentFoo(null,{bar:1}).hello(); } } 福班 { 公共函数新建(选项:{bar:Int}){} 公共函数hello() { trace(“你好”); } } 课堂流利 { 私有var父级:Null; 公共函数新建(父函数:Null) { this.parent=parent; } 公共职能结束() { 如果(父项!=null){ 返回父母; } 投掷“已达到最高水平”; } }

考虑以下代码:

类测试
{
公共静态函数main()
{
var foo=new FluentFoo(null,{bar:1}).hello();
}
}
福班
{
公共函数新建(选项:{bar:Int}){}
公共函数hello()
{
trace(“你好”);
}
}
课堂流利
{
私有var父级:Null;
公共函数新建(父函数:Null)
{
this.parent=parent;
}
公共职能结束()
{
如果(父项!=null){
返回父母;
}
投掷“已达到最高水平”;
}
}
类FluentFoo扩展了Fluent
{
公共变量库:Foo;
新公共函数(父函数:Null,选项:{bar:Int})
{
超级(家长);
基本=新的Foo(选项);
}
公共函数hello()
{
base.hello();
归还这个;
}
}
我想自动生成FluentFoo之类的类

在伪haxe代码中:

import haxe.Constraints.Constructible;

class Test
{
    public static function main()
    {
        var foo = new Fluent<Foo>(null, { bar: 1 }).hello();
    }
}

class Foo
{
    public function new(options:{ bar:Int }) {}

    public function hello()
    {
        trace("Hi there");
    }
}

@:generic
@:genericBuild(FluentMacro.build())
class Fluent<T:Constructible<Dynamic -> Void>>
{
    private var parent:Null<Fluent>;
    private var base:T;

    public function new(parent:Null<Fluent>, options:Dynamic)
    {
        this.parent = parent;
        this.base = new T(options);
    }

    public function end()
    {
        if(parent != null) {
            return parent;
        }

        throw 'Top level already reached';
    }
}

class FluentMacro
{
    public static function build()
    {
        //Get "T" public methods
        //Add them to class calling this genericBuild method (in this example to Fluent_Foo)
        //Modify them so they return "this"
    }
}
导入haxe.Constraints.Constructible;
课堂测试
{
公共静态函数main()
{
var foo=new Fluent(null,{bar:1}).hello();
}
}
福班
{
公共函数新建(选项:{bar:Int}){}
公共函数hello()
{
trace(“你好”);
}
}
@:通用
@:genericBuild(FluentMacro.build())
课堂流利>
{
私有var父级:Null;
私有var基:T;
新公共函数(父函数:Null,选项:动态)
{
this.parent=parent;
this.base=新T(选项);
}
公共职能结束()
{
如果(父项!=null){
返回父母;
}
投掷“已达到最高水平”;
}
}
类FluentMacro
{
公共静态函数build()
{
//获取“T”公共方法
//将它们添加到调用此genericBuild方法的类中(在本例中为Fluent\u Foo)
//修改它们,使其返回“this”
}
}
我知道我不能像从
上下文中得到的那样使用
@:build

然而,我还没有完全理解haxe手册中关于通用构建的内容——它们与普通的
@:build
在“类型构建宏”一节中,但是
build
方法应该返回
ComplexType
,而不是一个字段数组。是否可以在
@:genericBuild
中添加字段


谢谢

这比我预想的要复杂一些,但我做到了。供其他人使用:

import haxe.Constraints.Constructible;

class Test
{
    public static function main()
    {
        var foo = new Fluent<Foo>(null, { bar: 1 }).hello();
    }
}

class Foo
{
    public function new(options:{ bar:Int }) {}

    public function hello()
    {
        trace("Hi there");
    }
}

@:generic
@:genericBuild(FluentMacro.build())
class Fluent<T:Constructible<Dynamic -> Void>>
{
    private var parent:Null<Fluent>;
    private var base:T;

    public function new(parent:Null<Fluent>, options:Dynamic)
    {
        this.parent = parent;
        this.base = new T(options);
    }

    public function end()
    {
        if(parent != null) {
            return parent;
        }

        throw 'Top level already reached';
    }
}

class FluentMacro
{
    public static function build()
    {
        //Get "T" public methods
        //Add them to class calling this genericBuild method (in this example to Fluent_Foo)
        //Modify them so they return "this"
    }
}