I am trying to write a macro for setting up a submodule in my package. Since this is called at precompile time, I cannot use eval
(otherwise I get InitError: Evaluation into the closed module ImportEval breaks incremental compilation because the side effects will not be permanent. This is likely due to some other module mutating ImportEval with eval during precompilation - don't do this.
I set up a minimal example to illustrate where I am struggling:
a, b, c = 1, 2, 3
macro showvars(vars...)
quote
for v in $(esc(vars))
# 1) desired output, but eval prohibited
println("$v = ", eval(v))
# 2) not desired output
println("$v = ", v)
# 3) errors: 'UndefVarError: `v` not defined'
# println("$v = ", $v)
# 4) does not work: for-loop only generates quotes
quote
println("$v = ", $v)
end
# 5) does not work: same as 2.
$(quote
println("$v = ", v)
end)
end
end
end
@showvars c a
The desired output is
c = 3
a = 1
Is it possible to achieve the same behavior as in 1) without using eval
somehow? I know I can do it by explicitly constructing the appropriate expression with Expr(...)
but I feel there should be an easier, more readable way to do it.