using MacroTools: postwalk
macro mymac(e)
postwalk(x -> (x isa Expr && x.head isa Symbol && x.head == :.
? esc(:($x isa String ? (return $x) : $x isa Int ? $(x).val : throw("invalid"))) : x), e)
end
@mymac function foo(v)
y = identity(v).ATTR
return y + 1
end
foo(3) # ERROR: LoadError: UndefVarError: v not defined
@macroexpand(@mymac function foo(v)
y = identity(v).ATTR
return y + 1
end)
shows
:(function Main.foo(var"#133#v")
#= C:\Users\Win10\source\repos\julia\idioms\test\problem53.jl:8 =#
#= C:\Users\Win10\source\repos\julia\idioms\test\problem53.jl:9 =#
var"#132#y" = if (identity(v)).ATTR isa String
return (identity(v)).ATTR
else
if (identity(v)).ATTR isa Int
(identity(v)).ATTR.val
else
throw("invalid")
end
end
#= C:\Users\Win10\source\repos\julia\idioms\test\problem53.jl:10 =#
return var"#132#y" + 1
end)
It looks to me like you have to adapt the escaping in your macro?