【发布时间】:2017-07-18 15:56:50
【问题描述】:
我在 Haxe 宏中生成了一个 Int 数组,并希望在其上应用一个函数,如下所示:
typedef ComponentIdArray = Array<Int>;
class MyMacros {
// Each descendant of 'Component' has static member 'id_'
// Convert array of classes to corresponding indices
public static macro function classesToIndices(indexClasses:Array<ExprOf<Class<Component>>>) {
var ixs = [for (indexClass in indexClasses) {macro $indexClass.id_ ;}];
return macro $a{ixs};
}
public static macro function allOf(indexClasses:Array<ExprOf<Class<Component>>>) {
var indices = macro Matcher.classesToIndices($a{indexClasses});
// FIXME
// FIXME 'distinct' is a function from thx.core -- DOES NOT WORK
//var indices2 = macro ($a{indices}.distinct());
return macro MyMacros.allOfIndices($indices);
}
public static function allOfIndices(indices:ComponentIdArray) {
trace(indices);
//... normal function
// currently I call indices.distinct() here
return indices.distinct();
}
}
用法:
class C1 extends Component {} // C1.id_ will be set to 1
class C2 extends Component {} // C2.id_ will be set to 2
var r = MyMacros.allOf(C1, C2, C1); // should return [1,2]
由于一切都是已知的编译时,我想在宏中执行此操作。
【问题讨论】:
-
你想在宏上下文还是运行时上下文中应用函数?
-
“宏上下文”是否意味着编译时间?好的!基本上,我想在宏中执行此操作的原因是以上所有内容都是已知的comile-time。
-
虽然答案可能已经解决了,这篇文章也可能对code.haxe.org/category/macros/build-arrays.html有所帮助
-
马克,谢谢你的贡献!实际上,我已经以那篇文章为起点来生成我的值数组。继续努力!