Hej,
A new filt3rek’s topic as i know you all have been waiting for it !
I never really know how to use haxe.macro.Context.currentPos()
and what consequencies it can have if it’s not well done, I mean is it only for error reporting or does it have more impacts ? (Maybe if someone can give me some examples of impacts ?)
I’m talking about that because i’m trying to define a new type in onAfterTyping
macro context, this type has a @:build
meta on it, so it’s executed but in this context, when I try to get another type that is already typed, I can’t reach it.
Maybe a minimal example will be more clear : Try Haxe !
Macro.hx :
import haxe.macro.Context;
import haxe.macro.Expr;
import haxe.macro.TypeTools;
class Macro {
static var first = true;
public static function test() {
var cl = TypeTools.getClass(Context.getType("A")); // Works
haxe.macro.Context.onAfterTyping(_ -> {
if (first) {
var newT = {
pack: cl.pack,
name: "B",
meta: cl.meta.get().concat([{name: ':build', params: [macro Macro.build()], pos: Context.currentPos()},]),
fields: [],
kind: {
var superClassTP = cl.superClass != null ? @:privateAccess haxe.macro.TypeTools.toTypePath(cl.superClass.t.get(),
cl.superClass.params) : null;
TDClass(superClassTP, cl.interfaces.map(i -> @:privateAccess haxe.macro.TypeTools.toTypePath(i.t.get(), i.params)), cl.isInterface,
cl.isFinal, cl.isAbstract);
},
pos: cl.pos
}
Context.defineType(newT, cl.module);
first = false;
}
});
return null;
}
public static function build() {
trace(Context.getType("A")); // Doesn't Work : Uncaught exception Type not found 'A'
//trace(Context.getType("Test.A")); // Works
return null;
}
}
Test.hx :
class A {}
@:build(Macro.test())
class Test {
static function main() {
trace(A);
}
}
If someone can enlighten me ?
Note : I use here a build macro because in try.haxe we can’t set an init macro, but I think it’s ok with this example…
Thanks