In an attempt to level up my very basic Haxe macro skills, I want to find a macro solution where I can replace the follwing…
var active:Bool = true;
var activeStr:String = active ? "active" : "";
to a macro solution something like this:
var active:Bool = true;
var activeStr = active.boolToString();
// if true returns "active"
// if false returns ""
The variable name “active” is known at compile time, so no problem getting that using a macro like this:
import haxe.macro.Expr;
function main() {
var active:Bool = true;
var varName:String = getVariableName(active); // "active"
}
macro function getVariableName(e:ExprOf<Bool>)
return switch e.expr {
case EConst(CIdent(s)): macro $v{s};
default: throw 'Expr must be a single var name';
}
But the actual boolean value of the variable “active” isn’t known at compile time, so - as far as I understand it - I must create a macro that “generates the code needed for evaluating the value runtime value” - and here my knowledge ends.
I tried the following naive approach - with bad success, simply because the actual expression that goes into the getVariableName() macro method is nothing but the variable name “val”…
function main() {
var active = true;
var activeStr:String = boolToString(active);
trace(activeStr); // gives "val", not "active"
}
function boolToString(val:Bool):String
return val ? getVariableName(val) : "";
. . .
So, I guess that the macro must produce som some kind of runtime switch or if-statement that delivers the desired result… Please enligthen me!
Thank you in advance!
Jonas