Alias for a macro


#1

How can I alias a macro?
I would like to use @dot instead of @..


#2

You could do

macro dot(expr)
    :(@. $expr))
end

There is also the predefined alias @__dot__.


#3

Thanks!


#4

Oops.
@. seems to be more elaborate than the above definition of @dot.
It also works for in-place assignments for which @dot gives an error:

x = [0,1,2]
y = zeros(3)
@dot y = exp(x)

#5

You need to use esc correctly.


#6
macro dot(expr)
    esc(:(@. $expr))
end
  1. Is this correct?
  2. Is this the simplest way to alias a macro?

#7

No. On 0.6 the correct and preferred way is :(@. $(esc(expr))), i.e. you must escape each input once and exactly once. On master this is broken so the only way you can do this is esc(:($Base.@. $(esc(expr))))

You can also just do @eval const $(Symbol("@dot") = $(Symbol("@__dot__")))


#8

Thank you very much for the detailed explanation.

There was a small typo in parenthesizing, but the last version also works:
@eval const $(Symbol("@dot")) = $(Symbol("@__dot__"))