Alias for a macro

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

You could do

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

There is also the predefined alias @__dot__.

Thanks!

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)
1 Like

You need to use esc correctly.

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

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__")))

5 Likes

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__"))

2 Likes

Despite this old thread, perhaps it deserves to mention that a more convenient way is to use the var"name" syntax introduced in Julia 1.3.

julia> var"@myassert" = var"@assert";
julia> @myassert 1 + 1 == 3 "wrong equation"
ERROR: AssertionError: wrong equation

See here and this Stackoverflow for details.

5 Likes

Thank you for sharing this useful info!

1 Like

const var"@myassert" = var"@assert"

5 Likes