# Redefining a call expression inside a macro

**URL:** <https://discourse.julialang.org/t/redefining-a-call-expression-inside-a-macro/11428>\
**Category:** General Usage\
**Created:** [June 5, 2018, 3:20am UTC](https://discourse.julialang.org/t/redefining-a-call-expression-inside-a-macro/11428 "2018-06-05T03:20:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [June 5, 2018, 3:20am UTC](https://discourse.julialang.org/t/redefining-a-call-expression-inside-a-macro/11428/1 "2018-06-05T03:20:06Z")

</div>

Edit: In the category of “never mind…” 😊, the example below does not accurately replicate the problem.  
My apologies to those who took time to consider it.

Consider

```nohighlight
macro x(expr)
    dump(expr)
    println("------")
    dump(expr.args[1])
    println("======")

    expr.args[1] = :z
end

```

```nohighlight
julia> @x y()
Expr
  head: Symbol call
  args: Array{Any}((1,))
    1: Symbol y
  typ: Any
------
Symbol y
======
UndefVarError: z not defined

```

Why is `expr` evaluated when `expr.args[1] = :z`?

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [June 5, 2018, 4:08am UTC](https://discourse.julialang.org/t/redefining-a-call-expression-inside-a-macro/11428/2 "2018-06-05T04:08:45Z")

</div>

In a `macro` the return expression is evaluated. So the last line of code `expr.args[1] = :z` is the return value of the macro, and therefore `:z` is the expression that is evaluated.

---

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [June 5, 2018, 4:25am UTC](https://discourse.julialang.org/t/redefining-a-call-expression-inside-a-macro/11428/3 "2018-06-05T04:25:11Z")

</div>

Indeed. In Jupyter the following works.

```nohighlight
z()="hello world"

macro x(expr)
    dump(expr)
    println("------")
    dump(expr.args[1])
    println("======")

    expr.args[1] = :z
end

```

```nohighlight
julia> @x y()
Expr
  head: Symbol call
  args: Array{Any}((1,))
    1: Symbol y
  typ: Any
------
Symbol y
======
Out[1]:
z (generic function with 1 method)

```

But…

```nohighlight
Module MyModule
    z()="hello world"

    macro x(expr)
        dump(expr)
        println("------")
        dump(expr.args[1])
        println("======")

        expr.args[1] = :z
    end

    export @x, z
end

```

```julia
julia> using MyModule

julia> @x y()
UndefVarError: MyModule.z not defined
```

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [June 5, 2018, 4:37am UTC](https://discourse.julialang.org/t/redefining-a-call-expression-inside-a-macro/11428/4 "2018-06-05T04:37:08Z")

</div>

When I try out both examples, I get the same result for both and no error.

```nohighlight
julia> using MyModule

julia> @x y()
Expr
  head: Symbol call
  args: Array{Any}((1,))
    1: Symbol y
  typ: Any
------
Symbol y
======
z (generic function with 1 method)

```

When I run that, I don’t get the same `UndefVarError` you got, I get same result as first example.

---

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [June 5, 2018, 4:54am UTC](https://discourse.julialang.org/t/redefining-a-call-expression-inside-a-macro/11428/5 "2018-06-05T04:54:33Z")

</div>

Hmm…  
Thank you for looking into this.  
To be continued tomorrow…

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [June 5, 2018, 6:02am UTC](https://discourse.julialang.org/t/redefining-a-call-expression-inside-a-macro/11428/6 "2018-06-05T06:02:45Z")

</div>

Just a sanity check. This

> [@jandehaan](#):
>
> ```julia
> macro x(expr)
> dump(expr)
> println("------")
> dump(expr.args[1])
> println("======")
> 
> expr.args[1] = :z
> end
> 
> ```

looks like a complicated way to write

```julia
macro x(expr)
    return :z 
end

```

Did you mean

```julia
macro x(expr)
    expr.args[1] = :z
    return expr
end

```

?

---

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [June 5, 2018, 2:29pm UTC](https://discourse.julialang.org/t/redefining-a-call-expression-inside-a-macro/11428/7 "2018-06-05T14:29:19Z")

</div>

@chakravala and @GunnarFarneback, thank you for trying to help out. Unfortunately my “Minimum Working Example” did not accurately recreate the problem. My apology.  
Based on your feedback I dug deeper and the actual problem was not where I thought it was. There was another statement (not in the MWE) that evaluated expr.args[1] before returning it. Therefore the evaluation occurred in the wrong context. Problem solved.
