Let’s say I want to provide users with the above macro:
macro customLoop(controlExpr,workExpr)
return quote
for i in $controlExpr
$workExpr
end
end
end
If this macro is evaluated in Main then the user can just use the above code and everything is fine:
finalLoopIdx = 4
@customLoop 1:finalLoopIdx println(i)
If I however set the macro inside a module, e.g.:
module foo
export @customLoop2
macro customLoop2(controlExpr,workExpr)
return quote
for i in $controlExpr
$workExpr
end
end
end
end
Then the above code would NOT work, as finalLoopIndex
is out of scope for the macro (maybe this is the issue with the so-called “macro hygiene” ???):
using .foo
@customLoop2 1:finalLoopIndex println(i) # UndefVarError: finalLoopIndex not defined
How can a package can provide a macro that behaves like it is in the same module (e.g. Main
) of the user, i.e. it has access to the variables into the user’s current scope ?
Are there other ways to “solve” this kind of task ?