Macro constant arg inline final

Hej,

Sorry for flooding forum these days but I have another question about constant arguments for macro functions and haven’t found any solution anywhere.
Is there a way to use a constant for a macro function argument that is an identifier but inlined and final without doing extra switch ?
Something like that doesn’t work:

static inline final s = "foo";
...
myMacroFunction( s );
...
macro public static function myMacroFunction( str : String ){...}

Good question

Update: Yes! There is a built-in way to support this, converting to a typed expression seems to inline variables, which is nice. You can then convert back to regular Expr so you can use ExprTools.getValue

/**
 Return a value for an expression passed into a macro, even if that expression references other variables (so long as those variables are declared with inline)
**/
function evalConstExpr(x: Expr) {
	return try {
		ExprTools.getValue(Context.getTypedExpr(Context.typeExpr(x)));
	} catch (e) {
		Context.error("Must be a constant expression (i.e. and expression containing only constants or variables declared with `inline`", x.pos);
	}
}

Example: https://try.haxe.org/#D15683E2

evalConstExpr() returns a value for a given input expression, resolving inline vars in the process

Your macro would become

macro public static function myMacroFunction( exprStr : ExprOf<String> ){
    var str = evalConstExpr(exprStr);
    ...
}
1 Like

Thanks George to taking time to dig that !