@:build我正在尝试使用和宏向类及其子类的所有实例添加静态变量和静态函数@:autoBuild。
我设法让静态变量工作,但我不知道如何从各种“构建”函数EFunction,EFor等等。
这是我到目前为止的代码:
macro static public function addGetId() :Array<Field>
{
var fields : Array<Field> = Context.getBuildFields();
// The static _id field
var idField = {
name : "_id",
doc : null,
meta : [],
access : [AStatic, APrivate],
kind : FVar(macro : Int, macro -1),
pos : Context.currentPos()
};
// The getId function field
var getIdField = {
name : "getId",
doc : "Returns the ID of this command type.",
meta : [],
access : [AStatic, APublic],
kind : FFun({
params : [],
args : [],
expr: // What do I have to write here???
ret : macro : Int
}),
pos : Context.currentPos()
};
fields.push(idField);
fields.push(getIdField);
return fields;
}
Run Code Online (Sandbox Code Playgroud)
如果我想要添加的函数实际上位于 .hx 文件中,那么它在普通代码中的样子如下:
public static function getId() : Int
{
if (_id == -1)
{
_id = MySingleton.getInst().myGreatFunction()
}
return _id;
};
Run Code Online (Sandbox Code Playgroud)
因此它引用了新添加的_id变量以及一些单例类函数。
那么:完整的是什么getIdField()样子的?
额外问题:
我最大的问题是完全缺乏有关这些功能的文档以及手册中的任何有用示例。有没有任何实际有用的教程来创建这样的函数?
奖金奖金问题:和in和
有什么不一样? paramsargsFFun
您可以利用具体化来编写函数体,就像在常规 Haxe 代码中一样:
expr: macro {
if (_id == -1) {
_id = 0;
}
return _id;
},
Run Code Online (Sandbox Code Playgroud)
params是类型参数列表,args是函数接收的参数列表。Haxe 手册上有一个关于此的琐事部分:
琐事:参数与参数
在一些其他编程语言中,实参和形参可以互换使用。在 Haxe 中,argument用于引用方法,parameter指类型参数。