The docs are clear about how macros execute before the enclosing code is run, but I’m still having trouble imagining where this is necessary, when we already have functions that return expressions.
It’s easy to come up with examples like f_with_<>
below a macro like @m_xy
does the same thing as a function like f_xy
. Are there some alternative examples where macros would be necessary or provide some performance benefit?
macro m_xy(op,x,y)
return Expr(:call,op,x,y)
end
function f_xy(op,x,y)
return Expr(:call,op,x,y)
end
function f_with_mac()
@m_xy(+,a*x,a*y)
end
function f_with_func()
eval(f_xy(+,a*x,a*y))
end
a = 2
x = 3
y = 4
@test f_with_mac() == eval(f_with_func())