Injecting a local variable with macro

I was wondering if it was possible to inject a local variable using a macro. I tried this but it gives an error.

class Main {

    static function main() {
        // Works only if I uncomment this
        // var e;
        trace(e + 2); // Error - Unknown identifier : e

    @:keep static macro function injectLocal() {
        return macro var e = 5;

In the generated code I can see var e in the scope of main but I can’t access it from haxe code.

Should work with a macro in another file:

1 Like