# On macro hygiene

**URL:** https://discourse.julialang.org/t/on-macro-hygiene/20406
**Category:** General Usage
**Tags:** question
**Created:** [February 3, 2019, 6:08pm UTC](https://discourse.julialang.org/t/on-macro-hygiene/20406 "2019-02-03T18:08:49Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![kim366](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kim366/32/5756_2.png) [@kim366](https://discourse.julialang.org/u/kim366)
#### Post date: [February 3, 2019, 6:08pm UTC](https://discourse.julialang.org/t/on-macro-hygiene/20406/1 "2019-02-03T18:08:49Z")

</div>

So I have this macro:

```nohighlight
macro partial(call)
  Expr(:function, :((rest...,)), Expr(:call, call.args..., :(rest...)))
end

```

for partial function application.

```julia
timestwo = @partial *(2)
timestwo(5) # 10; works fine

rest = 7 # name `rest` clashes with parameter name in function generated by macro.

timesrest = @partial *(rest)
timesrest(5) # MethodError: no method matching *(::Tuple{Int64}, ::Int64)

```

if I `esc` the second expression (`Expr(:function, :((rest...,)), esc(Expr(:call, call.args..., :(rest...))))`) then it ignores the value of the parameter and uses the captured `rest` twice.

What is the best solution here? Is copying the args off the call even correct?

Thanks!

EDIT: I have misunderstood the use of `esc` as it is made to violate hygiene. `eval` solves the proble of name clashing, but when the variable `rest` is changed between the creation of `timesrest` and its invocation, the old value is used.

---

<div class="post-metadata">

### Author: ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)
#### Post date: [February 3, 2019, 7:16pm UTC](https://discourse.julialang.org/t/on-macro-hygiene/20406/2 "2019-02-03T19:16:06Z")

</div>

Escape the arguments:

```julia
macro partial(call)
    Expr(:function, :((rest...,)), Expr(:call, esc.(call.args)..., :(rest...)))
end

```

Now:

```julia
julia> rest = 7;

julia> timesrest = @partial *(rest);

julia> timesrest(5)
35

```

Note that I would probably not use a macro for this. I usually find it easier to read and work with other constructs, such as using anonymous functions directly in this case:

```julia
julia> timestwo = x -> 2 * x;

julia> timestwo(5)
10

julia> rest = 7;

julia> timesrest = x -> rest * x;

julia> timesrest(5)
35

```

---

<div class="post-metadata">

### Author: ![kim366](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kim366/32/5756_2.png) [@kim366](https://discourse.julialang.org/u/kim366)
#### Post date: [February 3, 2019, 7:24pm UTC](https://discourse.julialang.org/t/on-macro-hygiene/20406/3 "2019-02-03T19:24:59Z")

</div>

Awesome! Exactly what I needed. I know that in this case it doesn’t make much sense, but for a project I’m making it’ll be useful
