# How to correctly refer arguments of macro in nested quote?

**URL:** https://discourse.julialang.org/t/how-to-correctly-refer-arguments-of-macro-in-nested-quote/23144
**Category:** General Usage
**Tags:** question
**Created:** [April 14, 2019, 5:45pm UTC](https://discourse.julialang.org/t/how-to-correctly-refer-arguments-of-macro-in-nested-quote/23144 "2019-04-14T17:45:14Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![shipengcheng1230](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shipengcheng1230/32/4089_2.png) [@shipengcheng1230](https://discourse.julialang.org/u/shipengcheng1230)
#### Post date: [April 14, 2019, 5:45pm UTC](https://discourse.julialang.org/t/how-to-correctly-refer-arguments-of-macro-in-nested-quote/23144/1 "2019-04-14T17:45:14Z")

</div>

Suppose I just want to do something like `f(x, ind) = x[ind] = 5.0`, I could write a similar macro like this:

```julia
julia> macro foo1(data, index)
           esc(quote
               $data[$index] = 5.0
           end)
       end
@foo1 (macro with 1 method)

julia> d1 = zeros(10);

julia> @foo1(d1, 3)
5.0

julia> d1[3] == 5
true

```

However, if I want some intermediate variable involved in it, like:

```julia
function foo2(data, index)
    newindex = index * 2 # or other types of manipulation
    data[newindex] = 5.0
end

```

I try to write the following:

```julia
julia> macro foo2(data, index)
           newindex = gensym(:newindex)
           esc(quote
               $(newindex) = $(index) * 2
               quote
                   $$(data)[$$(newindex)] = 5.0
               end
           end)
       end
@foo2 (macro with 1 method)

julia> d2 = zeros(10);

julia> @macroexpand @foo2(d2, 3)
quote
    #= REPL[16]:4 =#
    ##newindex#365 = 3 * 2
    #= REPL[16]:5 =#
    (Core._expr)(:block, $(QuoteNode(:(#= REPL[16]:6 =#))), (Core._expr)(:(=), (Core._expr)(:ref, d2, ##newindex#365), 5.0))
end

```

Notice that the final expr has `d2` in it instead of `:d2`, which the later is what I want. This isn’t setting the `d2` actually. How can I achieve this? I checked [this post](https://discourse.julialang.org/t/macro-hygiene-in-nested-quote-not-working/17806) but it doesn’t deal with this situation.

Thanks!

---

<div class="post-metadata">

### Author: ![shipengcheng1230](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shipengcheng1230/32/4089_2.png) [@shipengcheng1230](https://discourse.julialang.org/u/shipengcheng1230)
#### Post date: [April 14, 2019, 10:54pm UTC](https://discourse.julialang.org/t/how-to-correctly-refer-arguments-of-macro-in-nested-quote/23144/2 "2019-04-14T22:54:27Z")

</div>

I just solve this by following:

```julia
julia> macro foo4(data, index)
           quote
               newindex = $index * 2
               $(data)[newindex] = 5.0
           end
       end
@foo4 (macro with 1 method)

julia> d3 = zeros(10);

julia> @foo4(d3, 3)
5.0

julia> d3[6] == 5.0
true

```

where I got a great help from [this tutorial](https://riptutorial.com/julia-lang/example/24364/quotenode--meta-quot--and-expr--quote-).

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [April 15, 2019, 5:08am UTC](https://discourse.julialang.org/t/how-to-correctly-refer-arguments-of-macro-in-nested-quote/23144/3 "2019-04-15T05:08:22Z")

</div>

> [@shipengcheng1230](#):
>
> do something like `f(x, ind) = x[ind] = 5.0` , I could write a similar macro

I may be missing something, but you don’t need a macro for this.

---

<div class="post-metadata">

### Author: ![shipengcheng1230](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shipengcheng1230/32/4089_2.png) [@shipengcheng1230](https://discourse.julialang.org/u/shipengcheng1230)
#### Post date: [April 15, 2019, 1:42pm UTC](https://discourse.julialang.org/t/how-to-correctly-refer-arguments-of-macro-in-nested-quote/23144/4 "2019-04-15T13:42:04Z")

</div>

I was trying to implement a callback function to incrementally write the output to a hdf5 file in DifferentialEquations.jl, hence I need a macro to generate that function (binded with some static variable by using `let`). So this is one of the ingredients where it came from.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [April 15, 2019, 2:32pm UTC](https://discourse.julialang.org/t/how-to-correctly-refer-arguments-of-macro-in-nested-quote/23144/5 "2019-04-15T14:32:07Z")

</div>

I still don’t see why you need a macro, instead of a callable `struct` or closure.

---

<div class="post-metadata">

### Author: ![shipengcheng1230](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shipengcheng1230/32/4089_2.png) [@shipengcheng1230](https://discourse.julialang.org/u/shipengcheng1230)
#### Post date: [April 16, 2019, 12:56am UTC](https://discourse.julialang.org/t/how-to-correctly-refer-arguments-of-macro-in-nested-quote/23144/6 "2019-04-16T00:56:37Z")

</div>

You are right, I seem to make the problem more complicated. Nevertheless, this “detour” actually makes me learn something else.
