# Macro hygiene for macro m(ex...)

**URL:** <https://discourse.julialang.org/t/macro-hygiene-for-macro-m-ex/80859>\
**Category:** General Usage\
**Tags:** question, macros\
**Created:** [May 11, 2022, 5:21am UTC](https://discourse.julialang.org/t/macro-hygiene-for-macro-m-ex/80859 "2022-05-11T05:21:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sijun](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Sijun](https://discourse.julialang.org/u/Sijun)\
**Post date:** [May 11, 2022, 5:21am UTC](https://discourse.julialang.org/t/macro-hygiene-for-macro-m-ex/80859/1 "2022-05-11T05:21:35Z")

</div>

I have two macros defined as follows:

```julia
macro m1(ex)
    :($ex)
end    

macro m2(ex...)
    :($ex)
end  

```

And the results of macro expansions are:

```julia
julia> @macroexpand @m1 a, b
:((Main.a, Main.b))

julia> @macroexpand @m2 a, b
(:((a, b)),)

```

My question is why the symbols `:a` and `:b` are not qualified with module name in the case of `@m2` unlike to `@m1`?

Thank you!

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [May 11, 2022, 5:44am UTC](https://discourse.julialang.org/t/macro-hygiene-for-macro-m-ex/80859/2 "2022-05-11T05:44:08Z")

</div>

Not sure but your output isn’t equivalent either. The second one makes a tuple with another tuple inside, because a, b is parsed as one tuple arg in both macro invocations.

---

<div class="post-metadata">

**Author:** ![Sijun](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Sijun](https://discourse.julialang.org/u/Sijun)\
**Post date:** [May 11, 2022, 5:49am UTC](https://discourse.julialang.org/t/macro-hygiene-for-macro-m-ex/80859/3 "2022-05-11T05:49:07Z")

</div>

I know their outputs have to be different. In `m2` , `args` is slurping the one argument `(a, b)` into an one-element tuple, which is expected. My question is why `m2`, symbols are not being hygiened by macro expansion.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [May 11, 2022, 8:30am UTC](https://discourse.julialang.org/t/macro-hygiene-for-macro-m-ex/80859/4 "2022-05-11T08:30:23Z")

</div>

Ah now I get it, you’re inserting a Tuple into the AST in `@m2`, and this doesn’t go through macro hygiene because it is not an Expr or Symbol. You can embed anything you want in the AST in case you didn’t know, but it’s uncommon to do so.

```julia
julia> dump(@macroexpand @m1 a, b)
Expr
  head: Symbol tuple
  args: Array{Any}((2,))
    1: GlobalRef
      mod: Module Main
      name: Symbol a
    2: GlobalRef
      mod: Module Main
      name: Symbol b

julia> dump(@macroexpand @m2 a, b)
Tuple{Expr}
  1: Expr
    head: Symbol tuple
    args: Array{Any}((2,))
      1: Symbol a
      2: Symbol b

```

---

<div class="post-metadata">

**Author:** ![Sijun](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Sijun](https://discourse.julialang.org/u/Sijun)\
**Post date:** [May 11, 2022, 8:48am UTC](https://discourse.julialang.org/t/macro-hygiene-for-macro-m-ex/80859/5 "2022-05-11T08:48:58Z")

</div>

Oh, I didn’t know about that. Thank you so much!

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [May 11, 2022, 8:59am UTC](https://discourse.julialang.org/t/macro-hygiene-for-macro-m-ex/80859/6 "2022-05-11T08:59:37Z")

</div>

Ah I remembered one example which does this, the regex string macro. It inserts a Regex object into the AST so that it is already compiled at compile time.

```julia
julia> dump(@macroexpand r"abc")
Regex
  pattern: String "abc"
  compile_options: UInt32 0x040a0002
  match_options: UInt32 0x40000000
  regex: Ptr{Nothing} @0x00007fb0a0ce5ff0

```
