# String interpolation error

**URL:** <https://discourse.julialang.org/t/string-interpolation-error/6330>\
**Category:** New to Julia\
**Created:** [October 8, 2017, 8:40pm UTC](https://discourse.julialang.org/t/string-interpolation-error/6330 "2017-10-08T20:40:23Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [October 8, 2017, 8:40pm UTC](https://discourse.julialang.org/t/string-interpolation-error/6330/1 "2017-10-08T20:40:23Z")

</div>

I expected the following is equivalent to `AssertionError("1 == 1")`. But I was thrown an error.

```julia
julia> AssertionError($(string(:(1 == 1))))
ERROR: unsupported or misplaced expression $

```

Why am I wrong? Thanks!

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [October 8, 2017, 8:46pm UTC](https://discourse.julialang.org/t/string-interpolation-error/6330/2 "2017-10-08T20:46:20Z")

</div>

`AssertionError` takes a String argument (so not sure what is intention of `$` interpolation)

```julia
Julia-0.6.0> AssertionError(string(:(1 == 1)))
AssertionError("1 == 1")

```

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [October 8, 2017, 8:53pm UTC](https://discourse.julialang.org/t/string-interpolation-error/6330/3 "2017-10-08T20:53:38Z")

</div>

Thanks, @greg_plowman. In fact, I was trying to reproduce/mimick step by step part of the second line in the first example of the section “Building an advanced macro” on [this](https://docs.julialang.org/en/latest/manual/metaprogramming/#Building-an-advanced-macro-1) page. The example is about the macro `@assert`:

```julia
julia> macro assert(ex)
           return :( $ex ? nothing : throw(AssertionError($(string(ex)))) )
       end
@assert (macro with 1 method)

```

Still confused why a dollar sign is needed in the above example.

---

<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:** [October 8, 2017, 8:55pm UTC](https://discourse.julialang.org/t/string-interpolation-error/6330/4 "2017-10-08T20:55:14Z")

</div>

The dollar sign is used because the macro returns an `Expr`. Dollar sign means expression interpolation, so it is used inside of a `:(...)` quote block. Outside of a quote block, it is misplaced.

---

<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:** [October 8, 2017, 9:01pm UTC](https://discourse.julialang.org/t/string-interpolation-error/6330/5 "2017-10-08T21:01:44Z")

</div>

To make it more clear, In the example `ex` is interpolated into the the `return` expression of the `macro`. In your use case, you are not interpolating any expression.

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [October 8, 2017, 9:10pm UTC](https://discourse.julialang.org/t/string-interpolation-error/6330/6 "2017-10-08T21:10:01Z")

</div>

Thanks, @chakravala. I understand your explanation - `$` is needed because the macro returns an Expr.

I also tried the following:

```julia
julia> ex = :(1 == 1)
:(1 == 1)

julia> ex1 = :(AssertionError($(string(ex))))
:(AssertionError("1 == 1"))

julia> eval(ex1)
AssertionError("1 == 1")

julia> ex2 = :(AssertionError(string(ex)))
:(AssertionError(string(ex)))

julia> eval(ex2)
AssertionError("1 == 1")

```

Since `eval(ex1)` and `eval(ex2)` are identical, can I remove the dollar sign in the macro above? Thanks!!

---

<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:** [October 8, 2017, 9:17pm UTC](https://discourse.julialang.org/t/string-interpolation-error/6330/7 "2017-10-08T21:17:04Z")

</div>

> [@chobbes](#):
>
> Since eval(ex1) and eval(ex2) are identical, can I remove the dollar sign in the macro above? Thanks!!

They are only identical because you defined `ex` in the REPL, so evaluating them gives the same result. However, `ex1` can always be evaluated even if `ex` is not defined in your current scope, while `ex2` could not be evaluated if you do not have `ex` defined in your current scope. So whether you want to use the dollar sign depends on what context you need it for.

Also, what you posted is not a `macro`, it is an `Expr`.

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [October 8, 2017, 9:27pm UTC](https://discourse.julialang.org/t/string-interpolation-error/6330/8 "2017-10-08T21:27:20Z")

</div>

> [@chakravala](#):
>
> They are only identical because you defined ex in the REPL, so evaluating them gives the same result. However, ex1 can always be evaluated even if ex is not defined in your current scope, while ex2 could not be evaluated if you do not have ex defined in your current scope. So whether you want to use the dollar sign depends on what context you need it for.

Yes, I see. Thanks for the hints. In fact, I’m talking about this in the context of the macro example I posted above:

```julia
julia> macro assert(ex)
           return :( $ex ? nothing : throw(AssertionError($(string(ex)))) )
       end
@assert (macro with 1 method)

```

I can’t remove $ here, can I? Because? Thanks.

---

<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:** [October 8, 2017, 9:35pm UTC](https://discourse.julialang.org/t/string-interpolation-error/6330/9 "2017-10-08T21:35:33Z")

</div>

> [@chobbes](#):
>
> I can’t remove $ here, can I? Because? Thanks.

It wouldn’t work without the `$` there because `ex` is undefined when the macro evaluates its returned expression. The point of the `$` is to insert or “splice” the expression `ex` into the code that the macro evaluates.

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [October 8, 2017, 9:47pm UTC](https://discourse.julialang.org/t/string-interpolation-error/6330/10 "2017-10-08T21:47:10Z")

</div>

> [@chakravala](#):
>
> It wouldn’t work without the `$` there because ex is undefined when the macro evaluates its returned expression. The point of the `$` is to insert or “splice” the expression ex into the code that the macro evaluates.

Thanks! So, without `$`, `ex` will be expected to be a variable, which, of course, is undefined, right? With `$`, Julia knows that `ex` is a placeholder which will be interpolated/spliced at a later time? Am I right?

---

<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:** [October 8, 2017, 9:53pm UTC](https://discourse.julialang.org/t/string-interpolation-error/6330/11 "2017-10-08T21:53:24Z")

</div>

That is correct, the variable `ex` is only defined within the macro itself, but is not defined in the evaluation scope of the macro. So you need to insert the value of `ex` into the expression before it is evaluated, otherwise it will try to look for the value of `ex` when it is evaluting.

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [October 8, 2017, 9:56pm UTC](https://discourse.julialang.org/t/string-interpolation-error/6330/12 "2017-10-08T21:56:05Z")

</div>

Thank you so much for your help and the patience, buddy!!
