Acknowledgement: The solution about applying @:genericBuild
on functions is actually inspired by the haxetink libraries, in particular tink_json.
@:genericBuild in a nutshell
In Haxe, @:genericBuild
is a metadata that tags a class to be built with a macro, which is invoked for every occurrence of the class.
// ThreeDArray.hx
@:genericBuild(MyMacro.build())
class ThreeDArray<T> {}
// MyMacro.hx
class MyMacro {
public static function build() {
switch haxe.macro.Context.getLocalType() {
case TInst(_, [p]):
var complexType = haxe.macro.TypeTools.toComplexType(p);
return macro:Array<Array<Array<$complexType>>>;
default: throw 'assert'; // technically unreachable
}
}
}
// Then in somewhere:
var my3dIntArray:ThreeDArray<Int>;
var my3dStringArray:ThreeDArray<String>;
// etc...