# How to nest splicing contexts (i.e. @eval inside quote block)

**URL:** <https://discourse.julialang.org/t/how-to-nest-splicing-contexts-i-e-eval-inside-quote-block/38752>\
**Category:** General Usage\
**Tags:** eval\
**Created:** [May 4, 2020, 5:45pm UTC](https://discourse.julialang.org/t/how-to-nest-splicing-contexts-i-e-eval-inside-quote-block/38752 "2020-05-04T17:45:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zachmatson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zachmatson/32/9397_2.png) [@zachmatson](https://discourse.julialang.org/u/zachmatson)\
**Post date:** [May 4, 2020, 5:45pm UTC](https://discourse.julialang.org/t/how-to-nest-splicing-contexts-i-e-eval-inside-quote-block/38752/1 "2020-05-04T17:45:30Z")

</div>

I am trying to make a macro that needs to evaluate an arbitrary (not known at parse time) amount of iterations of some code, which will change each time. I am trying to figure out how you would separately splice into the different levels of quoting. Here is a rough example of this

```julia
macro somemacro(var1)
    return quote
        for var2 in foo($var1)
            @eval @othermacro $var2 $var1
        end
    end
end

```

I also saw [this](https://discourse.julialang.org/t/how-to-interpolate-in-nested-quotes/27964/2) post, but it didn’t really have an answer.

---

<div class="post-metadata">

**Author:** ![zachmatson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zachmatson/32/9397_2.png) [@zachmatson](https://discourse.julialang.org/u/zachmatson)\
**Post date:** [May 4, 2020, 5:49pm UTC](https://discourse.julialang.org/t/how-to-nest-splicing-contexts-i-e-eval-inside-quote-block/38752/2 "2020-05-04T17:49:24Z")

</div>

Also thought about trying to make `@othermacro` just capture `var2` directly instead of receiving it, to avoid this problem, but I can’t figure out how to make that work either.

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [May 4, 2020, 6:49pm UTC](https://discourse.julialang.org/t/how-to-nest-splicing-contexts-i-e-eval-inside-quote-block/38752/3 "2020-05-04T18:49:03Z")

</div>

> [@zachmatson](#):
>
> I am trying to make a macro that needs to evaluate an arbitrary (not known at parse time) amount of iterations of some code, which will change each time.

This sounds impossible. If the number of iterations is not known at parse time, it can’t be known to the macro either, since that’s when macros are expanded. In particular, in your code example `@othermacro` is expanded once, before the for loop is run.  
If you want to evaluate the macro a constant number of times (given as a literal) you can do

```julia
macro somemacro(n)
    Expr(:block, (:(@othermacro $i) for i=1:n)...)
end
macro othermacro(x)
    println("othermacro $x")
    :("othermacro",$x)
end

```

Now

```julia
julia> @macroexpand @somemacro 3
othermacro 1
othermacro 2
othermacro 3
quote
    ("othermacro", 1)
    ("othermacro", 2)
    ("othermacro", 3)
end

```

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [May 4, 2020, 8:20pm UTC](https://discourse.julialang.org/t/how-to-nest-splicing-contexts-i-e-eval-inside-quote-block/38752/4 "2020-05-04T20:20:40Z")

</div>

I’m a bit at a loss here, trying to understand what you want to do, and exactly what your problem is…

Do you think you could elaborate a bit on your example? In particular:

- what should the macro call look like?
- what should the macro expand to?

And IIUC the inner `@othermacro` macro is written by you. Does it have to be a macro? Is it called in an other context elsewhere, or did you just write it for the purpose of using it inside `@somemacro`?

---

<div class="post-metadata">

**Author:** ![zachmatson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zachmatson/32/9397_2.png) [@zachmatson](https://discourse.julialang.org/u/zachmatson)\
**Post date:** [May 5, 2020, 9:48pm UTC](https://discourse.julialang.org/t/how-to-nest-splicing-contexts-i-e-eval-inside-quote-block/38752/5 "2020-05-05T21:48:20Z")

</div>

I was basically trying to hack together a version of what `ImportMacros.jl` does and trying to manually bind the symbols from calling `names` on some module to local variables in the calling scope.

I think I pretty much figured out better ways to do this (export the actual for loop from the macro and abuse `Meta.parse` and the string constructor and `eval`), but with this question I am still curious if it is possible to have different depths of interpolation for nesting quoting calls. I’ve noticed that quote blocks leave string interpolations un-interpolated so that the actual interpolation can be done at evaluation time, but I can’t find any clear way for raw code in a quote block to be left un-interpolated so that a later quote block can pick it up.

---

<div class="post-metadata">

**Author:** ![zachmatson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zachmatson/32/9397_2.png) [@zachmatson](https://discourse.julialang.org/u/zachmatson)\
**Post date:** [May 5, 2020, 10:33pm UTC](https://discourse.julialang.org/t/how-to-nest-splicing-contexts-i-e-eval-inside-quote-block/38752/6 "2020-05-05T22:33:59Z")

</div>

Edit: There was a better way to do what I was doing by using a loop in the macro to build up the expressions and splatting it into another expression with `:toplevel` then just interpolating the whole thing into the output to avoid extra `eval` or `Parse` statements, so perhaps the failure to get that nested quoting to work was for the best, but I still think this is an area of ambiguity.
