I am trying to add a static variable and a static function to all instances of a class and its child classes using @:buildand macros @:autoBuild.
I managed to get a static variable to work, but I have no idea how to "build" a function from various EFunction, EForetc.
Here is the code I have:
macro static public function addGetId() :Array<Field>
{
var fields : Array<Field> = Context.getBuildFields();
// The static _id field
var idField = {
name : "_id",
doc : null,
meta : [],
access : [AStatic, APrivate],
kind : FVar(macro : Int, macro -1),
pos : Context.currentPos()
};
// The getId function field
var getIdField = {
name : "getId",
doc : "Returns the ID of this command type.",
meta : [],
access : [AStatic, APublic],
kind : FFun({
params : [],
args : [],
expr: // What do I have to write here???
ret : macro : Int
}),
pos : Context.currentPos()
};
fields.push(idField);
fields.push(getIdField);
return fields;
}
Here's what the function that I want to add in regular code would look like if it were actually in the .hx file:
public static function getId() : Int
{
if (_id == -1)
{
_id = MySingleton.getInst().myGreatFunction()
}
return _id;
};
Thus, it refers to a newly added variable _idas well as to some function of the singleton class.
So: How would getIdField()complete getIdField()?
:
- , - . , ?
:
params args FFun?