I want to make a macro that adds a field to my classes, and the field value should be Class<T>
of class they are sitting in. Here is my minimal example, and it does not compile with this error:
src/Base.hx:3: characters 2-11 : Unknown identifier : foo.Foo
But if I move Foo
from package foo
to root package, then it compiles and works.
Main.hx
import foo.Foo;
class Main
{
static function main()
{
trace(Foo.data);
}
}
Build.hx
import haxe.macro.Context;
import haxe.macro.Type;
import haxe.macro.Expr;
class Build
{
macro static public function build(DataClass):Array<Field>
{
var cls = Context.getLocalClass().get();
var pack = cls.pack.concat([cls.name]);
var name = pack.join(".");
trace(name);
var expr = {
expr: ExprDef.EConst(Constant.CIdent(name)),
pos: Context.currentPos()
}
var newFieldCls = macro class {
public static var data:Class<Dynamic> = $expr;
}
var fields = Context.getBuildFields();
return fields.concat(newFieldCls.fields);
}
}
Base.hx
package;
@:autoBuild(Build.build(Main.Data))
class Base
{
public function new()
{
}
}
foo/Foo.hx
package foo;
class Foo extends Base
{
}