# Undefined variable Error in a quote block

**URL:** <https://discourse.julialang.org/t/undefined-variable-error-in-a-quote-block/102404>\
**Category:** General Usage\
**Tags:** macros, metaprogramming\
**Created:** [August 2, 2023, 4:02pm UTC](https://discourse.julialang.org/t/undefined-variable-error-in-a-quote-block/102404 "2023-08-02T16:02:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![shawn\_T](https://avatars.discourse-cdn.com/v4/letter/s/858c86/32.png) [@shawn\_T](https://discourse.julialang.org/u/shawn_T)\
**Post date:** [August 2, 2023, 4:02pm UTC](https://discourse.julialang.org/t/undefined-variable-error-in-a-quote-block/102404/1 "2023-08-02T16:02:17Z")

</div>

Hi all,

I was designing a macro and the code below is the return line of the macro, but it reports “i not defined”, I think it happens at $(result[i]) because once I replace it with other expr it is fine, so is there a way to correctly use a index on a vector in a quote block?

```julia
return quote 
          for i in eachindex($result)
              push!($vect,$(result[i]))
          end
          $v = $vect 
       end

```

Thank you.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [August 2, 2023, 8:09pm UTC](https://discourse.julialang.org/t/undefined-variable-error-in-a-quote-block/102404/2 "2023-08-02T20:09:33Z")

</div>

What `$(result[i])` does is evaluate `result[i]` outside the quote expression inside the macro body, and then interpolate the value into the expression. `result` existed there but `i` didn’t, hence the error. `$result[i]` or `$(result)[i]` interpolate the `result` variable only.

Without the full macro I can’t really say this definitively, but the way you’re interpolating also seems problematic. One possible problem is the repeated interpolation. If you’re interpolating a non-expression, it’ll interpolate the same instance, which seems to be your intention; if you’re interpolating an expression, it’ll be evaluated to different instances:

```julia
julia> macro blah() vect = [1]; :($vect === $vect) end; @blah
true
julia> macro blah() vect = :([1]); :($vect === $vect) end; @blah
false

```

A more probable problem is interpolating something to the left side of an assignment. How did that not error in your case?

```julia
julia> macro blah2(x) v = 1; :($v = $x) end; @blah2(2)
ERROR: syntax: invalid assignment location "1"

```
