Is this expected behaviour?
What I'm trying to do is to generate constructors for subclasses(HpGroup) of a certain class(Group). To do that I need to add all fields to params of the constructor, but for some reason the inherited fields don't show up in the results of Context.getBuildFields(). Sure, right now I have only one property, that I can add manually, but what if I change Group?
Here's my code:
Group:
@:autoBuild(Macro.buildGroup())
class Group{
public var owner: Entity;
public function new(owner: Entity){
this.owner=owner;
}
}
HpGroup:
class HpGroup extends Group{
public var hp: IntWrapper;
public var maxHp: IntWrapper;
}
Macro.buildGroup():
macro public static function buildGroup(): Array<Field>{
var fields: Array<Field>=Context.getBuildFields();
//if user has not defined a constructor
if(!fields.exists(function(field){return field.name=="new";})){
var constrArgs: Array<FunctionArg>=[];
var exprs: Array<Expr>=[macro super(owner)]; //add super constructor call
//for every field: if its a var, add it to arguments and add an assignment to constructor
for(field in fields){
trace(field);
switch(field.kind){
case(FVar(type, _)):
constrArgs.push({ //create constructor arg
name: field.name,
type: type,
opt: false
});
if(field.name!="owner"){ //create assignment
exprs.push(macro $p{["this", field.name]}=$i{field.name});
}
default:
}
}
//add constructor to fields
fields.push({
name: "new",
pos: Context.currentPos(),
access: [APublic],
kind: FFun({
args: constrArgs,
expr: macro $b{exprs},
params: [],
ret: null
})
});
}
return fields;
}