In Julia, building a simple expression is as easy as inserting a few $s. For example if I write a macro that zeros a variable by setting it to literal 0:
macro zeroit(x::Symbol)
:($(esc(x)) = 0)
end
But if I want a zerothem macro that zeros multiple variables, I have to build an Expr manually:
macro zerothem(xs...)
e = Expr(:block)
e.args = map(collect(xs)) do x
:($(esc(x)) = 0)
end
e
end
Thank you for your replies! I am starting to understand it.
So $(exprs...) is a broadcasted macro interpolation, it will insert all elements from exprs into the parent expression. The key is that the “parent expression” needs to be made explicit (blocks, tuples, vectors, etc) so that the parser expects multiple sub-expressions:
In case of multiple independent expressions, we need quote $(exprs...) end or :($(exprs...);)