# How is macro arguments resolved?

**URL:** <https://discourse.julialang.org/t/how-is-macro-arguments-resolved/5293>\
**Category:** General Usage\
**Tags:** question, macros, metaprogramming\
**Created:** [August 9, 2017, 1:48am UTC](https://discourse.julialang.org/t/how-is-macro-arguments-resolved/5293 "2017-08-09T01:48:25Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chong\_Wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chong_wang/32/20307_2.png) [@Chong\_Wang](https://discourse.julialang.org/u/Chong_Wang)\
**Post date:** [August 9, 2017, 1:48am UTC](https://discourse.julialang.org/t/how-is-macro-arguments-resolved/5293/1 "2017-08-09T01:48:25Z")

</div>

In the official documentary, a macro example is provided:

```julia
macro time(ex)
    return quote
        local t0 = time()
        local val = $ex
        local t1 = time()
        println("elapsed time: ", t1-t0, " seconds")
        val
    end
end

```

After this macro, a problem is immediately brought up:

```julia
module MyModule
import Base.@time

time() = ... # compute something

@time time()
end

```

In the above snippet, `ex` in the macro definition will be resolved to the standard time function, not the one defined in the snippet.

This behavior seems to be quite strange to me. Naively, I would think since `ex` is obtained from macro calling environment, how come it is resolved in macro defining environment? How is macro arguments resolve exactly?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [August 9, 2017, 2:33am UTC](https://discourse.julialang.org/t/how-is-macro-arguments-resolved/5293/2 "2017-08-09T02:33:02Z")

</div>

Please finish reading that page of doc.

---

<div class="post-metadata">

**Author:** ![Chong\_Wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chong_wang/32/20307_2.png) [@Chong\_Wang](https://discourse.julialang.org/u/Chong_Wang)\
**Post date:** [August 9, 2017, 3:32pm UTC](https://discourse.julialang.org/t/how-is-macro-arguments-resolved/5293/3 "2017-08-09T15:32:24Z")

</div>

I finished the page before I asked. I know that the way out is to use `esc`, but I still don’t understand why `ex` should be resolved in macro defining environment. If I do

```julia
module MyModule
import Base.@time

myfun() = ... # compute something

@time myfun()
end

```

then `myfun` is not even defined in macro defining environment.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [August 9, 2017, 3:56pm UTC](https://discourse.julialang.org/t/how-is-macro-arguments-resolved/5293/4 "2017-08-09T15:56:24Z")

</div>

> [@Chong\_Wang](#):
>
> then myfun is not even defined in macro defining environment.

Right? What’s the problem? The base one should have the correct behavior and resolve to your function. The first example in the hygiene section is just demonstrating the problem and it is corrected a few lines later.

---

<div class="post-metadata">

**Author:** ![Chong\_Wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chong_wang/32/20307_2.png) [@Chong\_Wang](https://discourse.julialang.org/u/Chong_Wang)\
**Post date:** [August 9, 2017, 4:03pm UTC](https://discourse.julialang.org/t/how-is-macro-arguments-resolved/5293/5 "2017-08-09T16:03:47Z")

</div>

> [@yuyichao](#):
>
> at’s the problem? The base one should have the correct behavior and resolve to your function. The first example in the hygiene section is just demonstrating the problem and it is corrected a few lines later.

Does this mean macro arguments should always be escaped?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [August 9, 2017, 4:15pm UTC](https://discourse.julialang.org/t/how-is-macro-arguments-resolved/5293/6 "2017-08-09T16:15:30Z")

</div>

Not always but yes when you want them to be treated as normal code.

---

<div class="post-metadata">

**Author:** ![Chong\_Wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chong_wang/32/20307_2.png) [@Chong\_Wang](https://discourse.julialang.org/u/Chong_Wang)\
**Post date:** [August 9, 2017, 4:18pm UTC](https://discourse.julialang.org/t/how-is-macro-arguments-resolved/5293/7 "2017-08-09T16:18:28Z")

</div>

1. Can you give me an example when escape is not needed in defining macros? I cannot come up with even one example where macro arguments should not be resolved in macro calling environment.

2. Here comes another question: If macro arguments should almost always be escaped, why doesn’t the parser use this as the default behavior?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [August 9, 2017, 5:04pm UTC](https://discourse.julialang.org/t/how-is-macro-arguments-resolved/5293/8 "2017-08-09T17:04:19Z")

</div>

> [@Chong\_Wang](#):
>
> Can you give me an example when escape is not needed in defining macros? I cannot come up with even one example where macro arguments should not be resolved in macro calling environment.

Anything that deconstruct the expression passed in. Or does not treat the input as normal variables. You may also want to resolve some symbols in the macro’s environment. The cartisian loop macros, `@static`, `@compat`, `@pyimport` etc are all doing that to some degrees. In general, macros are way more powerful than simply wrapping some code in some enviroment. The macro can use all syntactic information in the function to do all what it want, which for any non-trivial transformation will not want the arguments to be escaped directly.

> [@Chong\_Wang](#):
>
> If macro arguments should almost always be escaped, why doesn’t the parser use this as the default behavior?

No macro arguments do not almost always need to be escaped. In many cases they’ll be escaped as pieces. Escaping pieces by default could be a better way but it’s not possible without huge breakage since it’s not at all under the runtime’s control currently, let alone the parser. Ref [RFC: WIP: Make macro hygiene easier to use and less error-prone by vtjnash · Pull Request #10940 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/10940)

---

<div class="post-metadata">

**Author:** ![Chong\_Wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chong_wang/32/20307_2.png) [@Chong\_Wang](https://discourse.julialang.org/u/Chong_Wang)\
**Post date:** [August 9, 2017, 5:45pm UTC](https://discourse.julialang.org/t/how-is-macro-arguments-resolved/5293/9 "2017-08-09T17:45:52Z")

</div>

Thank you for your explanation. @yuyichao

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [August 9, 2017, 6:28pm UTC](https://discourse.julialang.org/t/how-is-macro-arguments-resolved/5293/10 "2017-08-09T18:28:00Z")

</div>

It’s also possible to just [escape everything](https://discourse.julialang.org/t/looking-for-good-macro-examples/450/9) and use `gensym` to avoid name collisions.

---

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [August 10, 2017, 2:30am UTC](https://discourse.julialang.org/t/how-is-macro-arguments-resolved/5293/11 "2017-08-10T02:30:51Z")

</div>

And interpolate in any local variables and functions
