【发布时间】:2014-11-14 21:07:29
【问题描述】:
我正在尝试使用宏来转换一些变量声明:
function test():Void {
var someComp:Component = __SOME_MACRO__();
// Or...
@getCompById var someComp:Component;
// Or even simpler...
getCompById(someComp, Component); //do some fancy macro magic...
// Also, if it's not possible/easy with a variable ...
getCompById("someComp", Component); //with a string of the variable name.
}
...到这个:
function test() {
var someComp:Component = cast container.getCompById("someComp");
}
我更倾向于第三个选项(更短的语法,相同的结果)。
但我不知道如何编写宏(是否应该将字符串作为参数?表达式?)以及如何正确地将其作为宏表达式返回。
这是我目前得到的(损坏的)代码:
macro static function getCompById(someVar:Expr, typeVar:Expr) {
return macro {
var someVar:typeVar = cast container.getCompById("someVar");
};
}
有什么想法吗?
【问题讨论】:
标签: macros haxe local-variables compile-time