...
public static function build():Array<Field> {
var fields = Context.getBuildFields();
var extraFields = (macro class {
var index_:Int = -1;
public function index():Int return index_;
public function set_index(v:Int):Void index_ = v;
}).fields;
return fields.concat(extraFields);
}
}
...
@:build(IndexlMacro.build())
class Test {
}
Can i add an IndexI interface to class Test via a macro?
Sadly, no. You can add metadata, but not type information AFAIK.
Instead, I recommend using @:autoBuild.
@:autoBuild(IndexlMacro.build())
interface IndexI {
public function index():Int;
public function set_index(v:Int):Void;
}
Now if the user implements IndexI, Haxe will automatically add @:build(IndexlMacro.build()) to that class. It’s a little easier to explain to the user (just implement an interface, no need to use a metadata tag they’ve never heard of), and the macro is no longer responsible for adding implements IndexI.