# Expression interpolation

**URL:** <https://discourse.julialang.org/t/expression-interpolation/33472>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 17, 2020, 5:17am UTC](https://discourse.julialang.org/t/expression-interpolation/33472 "2020-01-17T05:17:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [January 17, 2020, 5:17am UTC](https://discourse.julialang.org/t/expression-interpolation/33472/1 "2020-01-17T05:17:59Z")

</div>

in [doc](https://docs.julialang.org/en/v1.3/manual/metaprogramming/#Interpolation-1), the following example is given:

```julia
julia> ex = :(a in $:((1, 2, 3) ) )
:(a in (1, 2, 3))

```

I don’t get the meaning of “expression interpolation”, as:

```julia
julia> ex2 = :(a in (1, 2, 3) )
:(a in (1, 2, 3))

julia> ex == ex2
true

```

why we need to do it in the way of `ex`, but not simply `ex2`? thanks.

---

<div class="post-metadata">

**Author:** ![Sijun](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Sijun](https://discourse.julialang.org/u/Sijun)\
**Post date:** [January 17, 2020, 6:18am UTC](https://discourse.julialang.org/t/expression-interpolation/33472/2 "2020-01-17T06:18:18Z")

</div>

I think the point is…everything inside `:()` is verbatim except for things wrapped inside $, which are evaluated beforehand. So, the following is “wrong”

```julia
t = (1,2,3)
ex = :(a in t)

```

but this is “right”:

```julia
t = (1,2,3)
ex = :(a in $t)

```

I think it is the very point the relevant section of the doc is making.

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [January 17, 2020, 7:31am UTC](https://discourse.julialang.org/t/expression-interpolation/33472/3 "2020-01-17T07:31:18Z")

</div>

interpolating a variable, as in your `$t` example, is well understood. But I have difficulty in understanding the interpolation of an **expression**.

e.g.

```julia
julia> :(a - (1 + 1) )
:(a - (1 + 1))

julia> :(a - $:( (1 + 1) ) )
:(a - (1 + 1))

```

seems like:

1. `:( )` would quote something, i.e. prevent the thing inside being evaluated.
2. `$` would evaluate the thing after it.

so, the question is: what’s the use of `$:( )`??? it seems to me that the quoting and interpolation just **cancels** each other and do nothing??? 😵

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [January 17, 2020, 11:48am UTC](https://discourse.julialang.org/t/expression-interpolation/33472/4 "2020-01-17T11:48:42Z")

</div>

In that example of the docs, there is no advantage to using interpolation. A better and more useful example should probably be provided. It is indeed a useless example, since it is not a useful usecase. However, the point of interpolation is that you want to evaluate or construct a piece of code before inserting it into the expression. Therefore, a better example could be made in the docs, where the usefulness is emphasized.

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [January 17, 2020, 12:37pm UTC](https://discourse.julialang.org/t/expression-interpolation/33472/5 "2020-01-17T12:37:55Z")

</div>

… so, could we have “a better example” here? thanks. 🙏

---

<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:** [January 17, 2020, 12:56pm UTC](https://discourse.julialang.org/t/expression-interpolation/33472/6 "2020-01-17T12:56:25Z")

</div>

> [@tomtom](#):
>
> what’s the use of `$:( )` ??? it seems to me that the quoting and interpolation just **cancels** each other and do nothing???

The example could be written as

```julia
julia> ex1 = :((1, 2, 3))
:((1, 2, 3))

julia> ex = :(a in $(ex1))
:(a in (1, 2, 3))

```

Since you are interpolating into an expression with `$`, you need another expression, which is constructed using `:`.

`Expr(:tuple, 1, 2, 3)` gives the same expression as `:(1, 2, 3)`, so one could have used

```julia
ex = :(a in $(Expr(:tuple, 1, 2, 3)))

```

It’s just slightly more cumbersome.

FWIW, I think that example is fine. It’s just that working with AST requires some exploration, so that chapter should be read from the begining, with the user experimenting with `dump` and all the examples there to understand expressions.

---

<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:** [January 17, 2020, 1:30pm UTC](https://discourse.julialang.org/t/expression-interpolation/33472/7 "2020-01-17T13:30:16Z")

</div>

(also, moved to _Usage_, working with ASTs is beyond _First steps_)

---

<div class="post-metadata">

**Author:** ![Duane\_Wilson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/duane_wilson/32/9506_2.png) [@Duane\_Wilson](https://discourse.julialang.org/u/Duane_Wilson)\
**Post date:** [January 17, 2020, 2:46pm UTC](https://discourse.julialang.org/t/expression-interpolation/33472/8 "2020-01-17T14:46:00Z")

</div>

I think it is probably important to note that interpolating a variables and expression in an expression lead to slightly different things.

```julia
julia> v = (1,2,3)
julia> dump(:(a in $v))
Expr
  head: Symbol call
  args: Array{Any}((3,))
    1: Symbol in
    2: Symbol a
    3: Tuple{Int64,Int64,Int64}
      1: Int64 1
      2: Int64 2
      3: Int64 3

```

```julia
julia> dump(:(a in (1,2,3)))

Expr
  head: Symbol call
  args: Array{Any}((3,))
    1: Symbol in
    2: Symbol a
    3: Expr
      head: Symbol tuple
      args: Array{Any}((3,))
        1: Int64 1
        2: Int64 2
        3: Int64 3

```

and if you don’t interpolate the expression you get.

```julia
julia> dump(:(a in :((1,2,3))))
Expr
  head: Symbol call
  args: Array{Any}((3,))
    1: Symbol in
    2: Symbol a
    3: Expr
      head: Symbol quote
      args: Array{Any}((1,))
        1: Expr
          head: Symbol tuple
          args: Array{Any}((3,))
            1: Int64 1
            2: Int64 2
            3: Int64 3

```

So really what it looks like interpolating the expression does is to unwrap the inner expression and pass it as an argument. I.E. The respective difference is:

```julia
julia> Expr(:call, :in, :a, (1,2,3))
:(a in (1, 2, 3))

julia> Expr(:call, :in, :a, :((1,2,3)))
:(a in (1, 2, 3))

julia> Expr(:call, :in, :a, Expr(:quote, :(1,2,3)))
:(a in $(Expr(:quote, :((1, 2, 3)))))

```
