# Interpolation of expression

**URL:** <https://discourse.julialang.org/t/interpolation-of-expression/27102>\
**Category:** General Usage\
**Tags:** metaprogramming\
**Created:** [August 2, 2019, 4:43am UTC](https://discourse.julialang.org/t/interpolation-of-expression/27102 "2019-08-02T04:43:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [August 2, 2019, 4:43am UTC](https://discourse.julialang.org/t/interpolation-of-expression/27102/1 "2019-08-02T04:43:19Z")

</div>

Is there a more elegant way of interpolating `x_i = i`, without having to work with strings?

This is what I am using now:

```julia
for i = 1:3
    eval(Meta.parse("x$i = $i"))
end

```

I haven’t figured out metaprogramming yet ☹.

---

<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:** [August 2, 2019, 6:00am UTC](https://discourse.julialang.org/t/interpolation-of-expression/27102/2 "2019-08-02T06:00:40Z")

</div>

```julia
[:($(Symbol("x" * string(i))) = $(i)) for i in 1:3]

```

but, unless this is only for an MWE, you are probably much better off with another structure, eg a `Vector`, or a `Dict`.

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [August 2, 2019, 6:08am UTC](https://discourse.julialang.org/t/interpolation-of-expression/27102/3 "2019-08-02T06:08:23Z")

</div>

Yes, that’s a MWE.

Wow, I thought that would be more succinct. I’d have to construct many `Symbols` that way, which would result in not very readable code.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 2, 2019, 11:08am UTC](https://discourse.julialang.org/t/interpolation-of-expression/27102/4 "2019-08-02T11:08:11Z")

</div>

> [@Tamas\_Papp](#):
>
> `Symbol("x" * string(i))`

`Symbol("x", i)` also works.

---

<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:** [August 2, 2019, 11:09am UTC](https://discourse.julialang.org/t/interpolation-of-expression/27102/5 "2019-08-02T11:09:54Z")

</div>

> [@amrods](#):
>
> I’d have to construct many `Symbols` that way, which would result in not very readable code.

When you run into problems like this, it is very likely that you are using a non-idiomatic solution, and may not even need metaprogramming, but it is hard to say more without some context.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [August 2, 2019, 11:19am UTC](https://discourse.julialang.org/t/interpolation-of-expression/27102/6 "2019-08-02T11:19:30Z")

</div>

There are lots of useful macros in Base.Cartesian for this sort of thing:

```julia
julia> @macroexpand @nextract 3 x d->2d
quote
    x_1 = 2
    x_2 = 4
    x_3 = 6
end

```
