我產生內部haXe的宏觀int數組,並希望在其應用的功能是這樣的:價值觀的陣列應用在函數內部HAXE宏觀
typedef ComponentIdArray = Array<Int>;
// 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();
}
// Usage
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]
既然一切被稱爲編譯時我會喜歡在宏中這樣做。
你想在宏觀背景下或在運行時環境應用的功能? –
宏觀上下文是否意味着編譯時間?好的!基本上,我想在宏觀上做到這一點的原因是以上所有內容都是已知的通信時間。 –
雖然答案可能已經解決,但本文可能也有幫助http://code.haxe.org/category/macros/build-arrays.html –