# Interpolate all variables in expression without using $ for each variable

**URL:** <https://discourse.julialang.org/t/interpolate-all-variables-in-expression-without-using-for-each-variable/33101>\
**Category:** General Usage\
**Tags:** metaprogramming\
**Created:** [January 8, 2020, 3:33pm UTC](https://discourse.julialang.org/t/interpolate-all-variables-in-expression-without-using-for-each-variable/33101 "2020-01-08T15:33:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rvignolo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rvignolo/32/33498_2.png) [@rvignolo](https://discourse.julialang.org/u/rvignolo)\
**Post date:** [January 8, 2020, 3:33pm UTC](https://discourse.julialang.org/t/interpolate-all-variables-in-expression-without-using-for-each-variable/33101/1 "2020-01-08T15:33:37Z")

</div>

Hi,

is there any alternative way to do this:

```nohighlight
x = 1
y = 2
z = 3
i = 1
j = 2
k = 3
expr = :($x + 2 * $y + $z + $i + $j + $k)

```

For example, something like:

```nohighlight
expr = @$ :(x + 2 * y + z + i + j + k)

```

Thanks!

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [January 8, 2020, 3:43pm UTC](https://discourse.julialang.org/t/interpolate-all-variables-in-expression-without-using-for-each-variable/33101/2 "2020-01-08T15:43:24Z")

</div>

```julia
Expr(:call, :+, x, y, z, i, j, k)

```

---

<div class="post-metadata">

**Author:** ![rvignolo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rvignolo/32/33498_2.png) [@rvignolo](https://discourse.julialang.org/u/rvignolo)\
**Post date:** [January 8, 2020, 3:59pm UTC](https://discourse.julialang.org/t/interpolate-all-variables-in-expression-without-using-for-each-variable/33101/3 "2020-01-08T15:59:21Z")

</div>

Thanks for your reply!

I am trying to make it more readable by removing the `$`s. Your suggestion is not more readable than just using the dollar signs.

For example, the expression I am actually trying to build is:

```nohighlight
expr = :($du[1] = $κ * $θ * $b + ($σ^2) / 2 * $α * ($b^2))

```

And your suggestion is to write the following

```nohighlight
Expr(:(=), (:ref, du, 1), (:call, :+, (:call, :*, κ, θ, b), (:call, :*, (:call, :/, (:call, :^, σ, 2), 2), 1, (:call, :^, b, 2))))

```

which is not more readable.

Thanks!

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [January 8, 2020, 4:09pm UTC](https://discourse.julialang.org/t/interpolate-all-variables-in-expression-without-using-for-each-variable/33101/4 "2020-01-08T16:09:00Z")

</div>

For your use, stick with the `$`s. It has the advantage of being easily understood by others. Even if nobody else would ever see the source, you may write other, more shared code that uses the same approach (I have found that being consistent with Julia techniques makes for faster writing of correct code over time).

---

<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:** [January 8, 2020, 4:25pm UTC](https://discourse.julialang.org/t/interpolate-all-variables-in-expression-without-using-for-each-variable/33101/5 "2020-01-08T16:25:24Z")

</div>

> [@rvignolo](#):
>
> is there any alternative way to do this:

Why are you trying to do this? What problem are you trying to solve that involves interpolating lots of variables into lots of expressions?

---

<div class="post-metadata">

**Author:** ![rvignolo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rvignolo/32/33498_2.png) [@rvignolo](https://discourse.julialang.org/u/rvignolo)\
**Post date:** [January 8, 2020, 5:34pm UTC](https://discourse.julialang.org/t/interpolate-all-variables-in-expression-without-using-for-each-variable/33101/6 "2020-01-08T17:34:02Z")

</div>

> [@stevengj](#):
>
> Why are you trying to do this? What problem are you trying to solve that involves interpolating lots of variables into lots of expressions?

Hi. Please, look at my reply to @fredrikekre.

---

<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:** [January 8, 2020, 5:39pm UTC](https://discourse.julialang.org/t/interpolate-all-variables-in-expression-without-using-for-each-variable/33101/7 "2020-01-08T17:39:16Z")

</div>

> [@rvignolo](#):
>
> Hi. Please, look at my reply to @fredrikekre.

You didn’t really explain your underlying problem. Why are you building lots of expressions like this, so many that you want a special syntax? Are you passing expressions around when you really should be passing functions?
