# Interpolating a Dict with Exprs inside a quoted expression

**URL:** <https://discourse.julialang.org/t/interpolating-a-dict-with-exprs-inside-a-quoted-expression/53265>\
**Category:** General Usage\
**Tags:** question, macros, metaprogramming\
**Created:** [January 13, 2021, 12:49am UTC](https://discourse.julialang.org/t/interpolating-a-dict-with-exprs-inside-a-quoted-expression/53265 "2021-01-13T00:49:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MasFlam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/masflam/32/20950_2.png) [@MasFlam](https://discourse.julialang.org/u/MasFlam)\
**Post date:** [January 13, 2021, 12:49am UTC](https://discourse.julialang.org/t/interpolating-a-dict-with-exprs-inside-a-quoted-expression/53265/1 "2021-01-13T00:49:48Z")

</div>

I am coding some macros and in one of them I end up with a `Dict{Symbol, Tuple{Symbol, Expr}}`, where the `Expr`s are all supposed to be interpolated into the expression returned by the macro. What I am doing right now is this:

```julia
dict = Dict{Symbol, Tuple{Symbol, Expr}}()
# some code, filling up the dict...
quote
    $(some_fn)(
        $arg,
        $arg2,
        Dict{Symbol, Tuple{Symbol, DataType}}(
            [p.first => (p.second[1], eval(p.second[2])) for p in $(dict)]
        )
    )
end

```

But this has the drawback of `eval` working in the topmost scope, and I want this macro usable at module-level or from the REPL. Is there a way to interpolate all of those Exprs from the Dict’s values into a Dict constructor? Or am I doing this wrong and maybe there’s a better way of achieving this?

---

<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:** [January 13, 2021, 2:25am UTC](https://discourse.julialang.org/t/interpolating-a-dict-with-exprs-inside-a-quoted-expression/53265/2 "2021-01-13T02:25:14Z")

</div>

You probably want something like:

```julia
quote
    $(some_fn)(
        $arg,
        $arg2,
        Dict{Symbol, Tuple{Symbol, DataType}}(
            $([:($(QuoteNode(p.first)) => ($(QuoteNode(p.second[1])), $(p.second[2]))) for p in dict]...)
        )
    )
end

```

This should interpolate the expressions into the quote directly, so you don’t have to ressort to using eval.

---

<div class="post-metadata">

**Author:** ![MasFlam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/masflam/32/20950_2.png) [@MasFlam](https://discourse.julialang.org/u/MasFlam)\
**Post date:** [January 13, 2021, 8:29am UTC](https://discourse.julialang.org/t/interpolating-a-dict-with-exprs-inside-a-quoted-expression/53265/3 "2021-01-13T08:29:29Z")

</div>

Thanks! I didn’t really realize that I can broadcast a vector/put a tuple into `$( )` and that it would work like that, good to know!

---

<div class="post-metadata">

**Author:** ![MasFlam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/masflam/32/20950_2.png) [@MasFlam](https://discourse.julialang.org/u/MasFlam)\
**Post date:** [January 13, 2021, 6:27pm UTC](https://discourse.julialang.org/t/interpolating-a-dict-with-exprs-inside-a-quoted-expression/53265/4 "2021-01-13T18:27:58Z")

</div>

Actually, I tested this with the macro being defined in a module in a file I included in the REPL and this doesn’t actually work, generating an `UndefVarError`. In your solution the `p.second[2]` expressions are evaluated inside the _macro_ scope, and not the _caller’s_ scope. If you define the macro inside the REPL then actually the macro’s scope uses symbols from the scope above, where all the stuff you define in the REPL lies, so the variables are found.  
In short, this doesn’t work because the interpolated expressions are evaluated in the wrong scope.

---

<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:** [January 13, 2021, 7:27pm UTC](https://discourse.julialang.org/t/interpolating-a-dict-with-exprs-inside-a-quoted-expression/53265/5 "2021-01-13T19:27:55Z")

</div>

If you are returning this from a macro, you need to use `esc` to allow macro hygiene to resolve these bindings correctly, see:

[https://docs.julialang.org/en/v1/manual/metaprogramming/#Hygiene](https://docs.julialang.org/en/v1/manual/metaprogramming/#Hygiene)

---

<div class="post-metadata">

**Author:** ![MasFlam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/masflam/32/20950_2.png) [@MasFlam](https://discourse.julialang.org/u/MasFlam)\
**Post date:** [January 13, 2021, 8:25pm UTC](https://discourse.julialang.org/t/interpolating-a-dict-with-exprs-inside-a-quoted-expression/53265/6 "2021-01-13T20:25:14Z")

</div>

OK, now all’s working, thanks for help!
