# Args... in macros

**URL:** <https://discourse.julialang.org/t/args-in-macros/22256>\
**Category:** General Usage\
**Tags:** macros\
**Created:** [March 23, 2019, 8:39pm UTC](https://discourse.julialang.org/t/args-in-macros/22256 "2019-03-23T20:39:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![joaquimg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joaquimg/32/223_2.png) [@joaquimg](https://discourse.julialang.org/u/joaquimg)\
**Post date:** [March 23, 2019, 8:39pm UTC](https://discourse.julialang.org/t/args-in-macros/22256/1 "2019-03-23T20:39:53Z")

</div>

I have a macro: `@m1` which takes `args...` as argument as in:

```julia
macro m1(args...)
...
end

```

I wanted to create e new macro `@m2` which calls `@m1` with an extra parameter.  
That is, I want `@m2(a[1:3] = 3, key = :b)` to be equivalent to `@m1(a[1:3] = 3, key = :b, MyType())`

The best I could do was:

```julia
macro m2(args..)
    return :(@m1($(args...), MyType()))
end

```

But as expected I have problem with scoping (I need to use `esc`…).  
Unfortunately, things like:

```julia
macro m2(args..)
    return :(@m1($(esc(args)...), MyType()))
end

```

fail…

It look so close to the final solution, but it fails…  
Any ideas?

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [March 23, 2019, 8:42pm UTC](https://discourse.julialang.org/t/args-in-macros/22256/2 "2019-03-23T20:42:55Z")

</div>

Make a function `m1_fn`:

```julia
macro m1(args...)
  return m1_fn(args...)
end
m1_fn(args...) = ...

```

then call that from `m2`.

---

<div class="post-metadata">

**Author:** ![joaquimg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joaquimg/32/223_2.png) [@joaquimg](https://discourse.julialang.org/u/joaquimg)\
**Post date:** [March 23, 2019, 8:48pm UTC](https://discourse.julialang.org/t/args-in-macros/22256/3 "2019-03-23T20:48:13Z")

</div>

Thanks!  
Unfortunately, `m1` belongs to another package, so that would not be a nice solution…

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [March 23, 2019, 8:53pm UTC](https://discourse.julialang.org/t/args-in-macros/22256/4 "2019-03-23T20:53:30Z")

</div>

Ok, then `Base.marcoexpand(@ __MODULE__ , @m1($(args...))` to get the AST and take it from there.

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [March 23, 2019, 9:44pm UTC](https://discourse.julialang.org/t/args-in-macros/22256/5 "2019-03-23T21:44:23Z")

</div>

> [@joaquimg](#):
>
> Unfortunately, things like:
> 
> ```julia
> macro m2(args..)
> return :(@m1($(esc(args)...), MyType()))
> end
> 
> ```
> 
> fail…

Isn’t this just an issue with your use of `esc` on a tuple without broadcasting?

This seems to work fine (even if `@m1` is in another module):

```julia
module A
export @m1
macro m1(args...)
    quote
        println($(esc.(args)...))
    end
end
end # module A

macro m2(args...)
    quote
        @m1($(esc.(args)...), 4)
    end
end

```

```julia
julia> using .A

julia> @macroexpand @m1 1 2 3
quote
    #= REPL[1]:5 =#
    (Main.A.println)(1, 2, 3)
end

julia> @macroexpand @m2 1 2 3
quote
    #= REPL[2]:3 =#
    begin
        #= REPL[1]:5 =#
        (Main.A.println)(1, 2, 3, 4)
    end
end

```

and it seems behave correctly from an hygiene point of view:

```julia
julia> let x = 3
           @macroexpand @m2 1 2 x
       end
quote
    #= REPL[2]:3 =#
    begin
        #= REPL[1]:5 =#
        (Main.A.println)(1, 2, x, 4)
    end
end

julia> let x = 3
           @m2 1 2 x
       end
1234

```

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [March 24, 2019, 3:59am UTC](https://discourse.julialang.org/t/args-in-macros/22256/6 "2019-03-24T03:59:16Z")

</div>

It is currently nearly impossible to correctly handle hygiene with macros that call other macros. See [macro hygine escape stripped late for macros calling macros · Issue #23221 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/23221).

---

<div class="post-metadata">

**Author:** ![joaquimg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joaquimg/32/223_2.png) [@joaquimg](https://discourse.julialang.org/u/joaquimg)\
**Post date:** [March 24, 2019, 8:49pm UTC](https://discourse.julialang.org/t/args-in-macros/22256/7 "2019-03-24T20:49:03Z")

</div>

I was concluding something like that… Many thanks for the link!
