# How to interpolate symbol as symbol, not as variable name?

**URL:** <https://discourse.julialang.org/t/how-to-interpolate-symbol-as-symbol-not-as-variable-name/5158>\
**Category:** General Usage\
**Created:** [July 31, 2017, 10:56pm UTC](https://discourse.julialang.org/t/how-to-interpolate-symbol-as-symbol-not-as-variable-name/5158 "2017-07-31T22:56:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [July 31, 2017, 10:56pm UTC](https://discourse.julialang.org/t/how-to-interpolate-symbol-as-symbol-not-as-variable-name/5158/1 "2017-07-31T22:56:59Z")

</div>

Given a list of keys, I want to generate expressions for getting the keys from a predefined dictionary. E.g. for string keys:

```julia
ks = ["x", "y"]
result = [:(d[$k]) for k in ks]
# ==> 2-element Array{Expr,1}:                                                                                                                                                                                    
# ==> :(d["x"])                                                                                                                                                                                                         
# ==> :(d["y"])

```

Now let’s change key type to `Symbol`:

```julia
ks = [:x, :y]
result = [:(d[$k]) for k in ks]
# ==> 2-element Array{Expr,1}:                                                                                                                                                                                           
# ==> :(d[x])                                                                                                                                                                                                           
# ==> :(d[y])

```

Oops, Julia thinks I want variables `x` and `y` instead of constants `:x` and `:y`. Is there a way to fix it?

* * *

A couple of things I have already tried:

- `:(d[$(esc(k))])` gives `:(d[$(Expr(:escape, :x))])` which evaluates to “invalid syntax” error
- `:(d[Symbol("$k")])` gives `:(d[Symbol("$(k)")])` which evaluates to “k not defined” error

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [July 31, 2017, 11:11pm UTC](https://discourse.julialang.org/t/how-to-interpolate-symbol-as-symbol-not-as-variable-name/5158/2 "2017-07-31T23:11:42Z")

</div>

Either use `[:(d[$(QuoteNode(k)]) for k in ks]` or `[:(d[$(Expr(:quote, k))]) for k in ks]`. I think they’re equivalent…

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [July 31, 2017, 11:14pm UTC](https://discourse.julialang.org/t/how-to-interpolate-symbol-as-symbol-not-as-variable-name/5158/3 "2017-07-31T23:14:29Z")

</div>

Perfect, thanks!
