I have a macro that is supposed to define a new function in the caller’s scope (plus do several other things). The code to be evaluated is wrapped in a
try/catch block so that if something fails, I can clean things up. However, the functions get renamed by the macro expander (hygiene), even though the whole expression is wrapped in
macro test() fun_def = :( foo(x) = 2*x ) # ( ... modify some global state ... ) return esc( quote try # (... do some stuff that might fail ...) $fun_def catch e # ( ... revert the modification of the global state ... ) rethrow(e) end end ) end julia> @test (::getfield(Main, Symbol("#foo#13"))) (generic function with 1 method)
It has been suggested that this may have something to do with the local scope introduced by
try. I have tried various ways to make
fun_def escape hygiene, but none of them work. Any suggestions?