julia> @macroexpand @P "text"
quote
#= REPL[1]:3 =#
if Main.Print
#= REPL[1]:4 =#
Main.println("text")
end
end
that Print refers to Main.Print, which is a global that’s expected to exist in the Main module, but in f you want it to refer to a local variable called Print. You’ll need to esc the Print in your returned expression from the macro:
julia> macro P(ex)
return quote
if $(esc(:Print))
println($ex)
end
end
end
@P (macro with 1 method)
julia> @macroexpand @P "text"
quote
#= REPL[3]:3 =#
if Print
#= REPL[3]:4 =#
Main.println("text")
end
end
Wow, I knew it was something simple, in my case I understood esc, but not :Print vs. Print, I see the difference now. I will redouble my efforts to understand metaprogramming on a deeper level.
This is why Julia is so great, the helpfulness of the community. Thanks Mike.