On Sun, Jun 5, 2016 at 11:41 AM, Scott Jones <[email protected]>
wrote:
> On the other hand, that would prevent having a function with the same name
> as a macro.
This is the primary reason. When you define a macro named "@foo", the
transformation function for it is bound to the non-standard symbol "@foo":
julia> macro foo(x) esc(x) end
@foo (macro with 1 method)
julia> getfield(Main, Symbol("@foo"))(:(x + y))
:($(Expr(:escape, :(x + y))))
You can actually define a macro with the function syntax by using eval to
splice in a macro-like name for the function:
julia> @eval function $(Symbol("@foo"))(x) esc(x) end
@foo (macro with 1 method)
julia> macroexpand(:(@foo x + y))
:(x + y)
So the syntax for macro definition could be changed to something like this:
function @foo(x) esc(x) end
However this short form:
@foo(x) = esc(x)
would not work since it's ambiguous with having a macro call be the left
hand side of an assignment.