How can I get the identifier of a variable as a String?
For example:
var myVar:Int = 0;
var myFunction:Void -> Void;
How do I get “myVar” or “myFunction” as a String?
How can I get the identifier of a variable as a String?
For example:
var myVar:Int = 0;
var myFunction:Void -> Void;
How do I get “myVar” or “myFunction” as a String?
It’s pretty simple to do with an expression macro actually:
class Main {
static var field:Int = 0;
static function main() {
var localVar:Int = 0;
trace(nameof(field)); // field
trace(nameof(localVar)); // localVar
}
macro static function nameof(e:haxe.macro.Expr):haxe.macro.Expr {
return switch e.expr {
case EConst(CIdent(ident)): macro $v{ident};
case _: throw "identifier expected";
}
}
}