How do I get the current variable value within a macro?

macro getvar(x)
    println(x)
    return nothing
end

x = 1

@getvar x

getvar sees x as a symbol and prints out “x” as expected.

Now, let’s assume I need the x value for some further logic within the macro (not within the expression returned by the macro, but before I build the expression)? How do I get it?

You don’t, that’s the whole point of macros. They operate only on the syntax representation of code, and are run before the variable x ever gets a value.

10 Likes