I have a macro works if defined in main.
macro check_set(d,v)
nm = string(v)
quote
global $v
if haskey($d,$nm)
$v = $d[$nm]
end
end
end
However, when I put it inside a Module (macro defined inside the module) to use in a function (also defined inside the module), I get scope issues.
export setVars
function setVars(val::Dict)
global max_iter
@check_set val max_iter
end
In main I call setVars(config)
where config
is a dictionary as expected. I get:
ERROR: UndefVarError: val not defined
Adding @macroexpand
I see:
begin
#= /home/dpazzula/Documents/Stuff/src/Stuff.jl:156 =#
global max_iter
#= /home/dpazzula/Documents/Stuff/src/Stuff.jl:157 =#
if Stuff.haskey(Stuff.val, "max_iter")
#= /home/dpazzula/Documents/Stuff/src/Stuff.jl:158 =#
Stuff.max_iter = Stuff.val["max_iter"]
end
end
So the ERROR makes sense, it is looking for Stuff.val
when val
is locally scoped to the function.
How do I get it to look for the locally scoped val
?