# Why Julia can execute code when macros are expanded?

**URL:** https://discourse.julialang.org/t/why-julia-can-execute-code-when-macros-are-expanded/43987
**Category:** General Usage
**Tags:** question
**Created:** [July 30, 2020, 3:15pm UTC](https://discourse.julialang.org/t/why-julia-can-execute-code-when-macros-are-expanded/43987 "2020-07-30T15:15:21Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Hawk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hawk/32/16763_2.png) [@Hawk](https://discourse.julialang.org/u/Hawk)
#### Post date: [July 30, 2020, 3:15pm UTC](https://discourse.julialang.org/t/why-julia-can-execute-code-when-macros-are-expanded/43987/1 "2020-07-30T15:15:21Z")

</div>

Hi, when I read the section of metaprogramming in Tom Kwong’s Book “Hands-on Design Patterns and Best Practices with Julia”, I came across such an example:

```julia
macro identity(ex)
       dump(ex)
return ex end

```

And then when we define such a function in REPL as:

 ![example](https://global.discourse-cdn.com/julialang/original/3X/4/c/4c9a9cd5b8c5ba44354e29d48eece8b8a901848c.png)  
I am a little bit curious how Julia can execute the instruction dump(ex) when the macro is expanded, since we just define the function.  
Or can anyone explain the mechanism, when julia internally parses the macro like this which does not include instruction in an expression?

Thanks!

---

<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: [July 30, 2020, 5:00pm UTC](https://discourse.julialang.org/t/why-julia-can-execute-code-when-macros-are-expanded/43987/2 "2020-07-30T17:00:13Z")

</div>

Macro is just a special function, it’s whole point is to execute code to do expression transformation. That’s literally what “macro expansion” means in julia and it shouldn’t be a surprise that code execution happens.

If you want to know how it’s implemented, well, as I said, it’s jus a function that the runtime knows how to call. Iif you want to know why your code runs at expansion time but you expect it to run at runtime, then that’s because you need to return the new expression to do whatever you want at runtime. You need to return an expression that does `dump(ex)`. sth like `return $(dump($(QuoteNode(ex))); $(esc(ex)))`

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [July 30, 2020, 5:18pm UTC](https://discourse.julialang.org/t/why-julia-can-execute-code-when-macros-are-expanded/43987/3 "2020-07-30T17:18:16Z")

</div>

I think

> [@yuyichao](#):
>
> sth like `return $(dump($(QuoteNode(ex))); $(esc(ex)))`

I think you meant `return :(dump($(QuoteNode(ex))); $(esc(ex)))`

---

<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: [July 30, 2020, 5:57pm UTC](https://discourse.julialang.org/t/why-julia-can-execute-code-when-macros-are-expanded/43987/4 "2020-07-30T17:57:03Z")

</div>

Yes

---

<div class="post-metadata">

### Author: ![Hawk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hawk/32/16763_2.png) [@Hawk](https://discourse.julialang.org/u/Hawk)
#### Post date: [July 31, 2020, 2:48am UTC](https://discourse.julialang.org/t/why-julia-can-execute-code-when-macros-are-expanded/43987/5 "2020-07-31T02:48:05Z")

</div>

Thanks, but I am wondering if as you said in this case, the macro returns the expressions as a whole, how the compiler can recognize what part of code should be execute at the parse time and compiling time, as the example given in the manual:

```julia
julia> macro twostep(arg)
           println("I execute at parse time. The argument is: ", arg)
           return :(println("I execute at runtime. The argument is: ", $arg))
       end
@twostep (macro with 1 method)

```

```julia
julia> ex = macroexpand(Main, :(@twostep :(1, 2, 3)) );
I execute at parse time. The argument is: :((1, 2, 3))

```

```julia

```

---

<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: [July 31, 2020, 4:58am UTC](https://discourse.julialang.org/t/why-julia-can-execute-code-when-macros-are-expanded/43987/6 "2020-07-31T04:58:54Z")

</div>

None of the code returned runs at parse time. Your first println is not part of the code returned. It’s just the body of the function.

---

<div class="post-metadata">

### Author: ![Hawk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hawk/32/16763_2.png) [@Hawk](https://discourse.julialang.org/u/Hawk)
#### Post date: [August 1, 2020, 9:04am UTC](https://discourse.julialang.org/t/why-julia-can-execute-code-when-macros-are-expanded/43987/7 "2020-08-01T09:04:41Z")

</div>

Thanks, I understand.
