# Alias two macros

**URL:** <https://discourse.julialang.org/t/alias-two-macros/113028>\
**Category:** General Usage\
**Created:** [April 16, 2024, 4:41pm UTC](https://discourse.julialang.org/t/alias-two-macros/113028 "2024-04-16T16:41:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mancolric](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mancolric/32/21799_2.png) [@mancolric](https://discourse.julialang.org/u/mancolric)\
**Post date:** [April 16, 2024, 4:41pm UTC](https://discourse.julialang.org/t/alias-two-macros/113028/1 "2024-04-16T16:41:16Z")

</div>

I would like to define a macro such that `@mymacro expression` is the same as `@tturbo @. expression`. Is there any way to do this? I have read the documentation and made several attempts, but I am completely new to macros and I cannot figure it out.

---

<div class="post-metadata">

**Author:** ![Gianluca\_Fuwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gianluca_fuwa/32/46071_2.png) [@Gianluca\_Fuwa](https://discourse.julialang.org/u/Gianluca_Fuwa)\
**Post date:** [April 16, 2024, 4:57pm UTC](https://discourse.julialang.org/t/alias-two-macros/113028/2 "2024-04-16T16:57:08Z")

</div>

I have personally never done this, but looking at code from Trixi.jl ([Trixi.jl/src/auxiliary/auxiliary.jl at dd051566a9079e22dfa792b145c0b50eaafa37fd · trixi-framework/Trixi.jl · GitHub](https://github.com/trixi-framework/Trixi.jl/blob/dd051566a9079e22dfa792b145c0b50eaafa37fd/src/auxiliary/auxiliary.jl#L219)), they defined their own `@threaded` macro using `Polyester.@batch` as follows:

```julia
macro threaded(expr)
    #=.... comments .... =#
    return esc(quote
                   Trixi.@batch $(expr)
               end)
end

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [April 16, 2024, 4:57pm UTC](https://discourse.julialang.org/t/alias-two-macros/113028/3 "2024-04-16T16:57:35Z")

</div>

Composing macros — creating macros that call macros — is surprisingly (and unfortunately) challenging. AFAIK there’s not a general solution because you need to know each macro’s particular escaping and interpolating and syntax rules. There’s definitely ways to do this — hopefully someone here can help you with this particular use case — but it is generally not obvious.

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [April 16, 2024, 5:26pm UTC](https://discourse.julialang.org/t/alias-two-macros/113028/4 "2024-04-16T17:26:29Z")

</div>

Yes, macros can interact in unexpected way when each of them transforms the expression in some way (see [here](https://discourse.julialang.org/t/zero-based-indexing-not-again/100944/8) for an example).  
Yet, when inserting another macro into its expansion, the outer macro has control over the syntax and just needs to ensure that its valid for the inner one, i.e., that the generated code makes sense and works as expected. In this case, it should be rather straight-forward as the expression is just passed as is:

```julia
macro ha(expr)
    esc(:(LoopVectorization.@tturbo Base.@. $expr))
end

```

The only difficulty seems to be that macro names in the expansion are not automatically namespaced like function names, i.e., hygienic. Hence, the need to do this by hand and escaping the expression.

---

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [April 16, 2024, 9:36pm UTC](https://discourse.julialang.org/t/alias-two-macros/113028/5 "2024-04-16T21:36:55Z")

</div>

> [@bertschi](#):
>
> ```julia
> macro ha(expr)
> esc(:(LoopVectorization.@tturbo Base.@. $expr))
> end
> 
> ```

This macro escapes everything. If a user has imported his own package which is accidentally also named `LoopVectorization` and contains a macro `@tturbo`, then the user-defined `@tturbo` will be called by the outer macro, even if the outer macro `ha` is defined in a module in which the original (registered package) `LoopVectorization` is imported.

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [April 17, 2024, 4:34pm UTC](https://discourse.julialang.org/t/alias-two-macros/113028/6 "2024-04-17T16:34:35Z")

</div>

Right, and I had warned about escaping everything myself in another thread.  
Somehow, I got the impression that macro calls are resolved differently than function calls in quoted expressions. The following seems to work as expected though:

```julia
macro ha(expr)
    :(@tturbo @. $expr)
end

```

assuming that `@tturbo` has been imported into the module defining the macro.

---

<div class="post-metadata">

**Author:** ![mancolric](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mancolric/32/21799_2.png) [@mancolric](https://discourse.julialang.org/u/mancolric)\
**Post date:** [April 17, 2024, 6:57pm UTC](https://discourse.julialang.org/t/alias-two-macros/113028/7 "2024-04-17T18:57:21Z")

</div>

This worked. Thank you very much! 😀
