# A string interpolation inconsistency

**URL:** https://discourse.julialang.org/t/a-string-interpolation-inconsistency/14042
**Category:** General Usage
**Tags:** string-interpolation
**Created:** [August 25, 2018, 3:37pm UTC](https://discourse.julialang.org/t/a-string-interpolation-inconsistency/14042 "2018-08-25T15:37:43Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![sswatson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sswatson/32/5135_2.png) [@sswatson](https://discourse.julialang.org/u/sswatson)
#### Post date: [August 25, 2018, 3:37pm UTC](https://discourse.julialang.org/t/a-string-interpolation-inconsistency/14042/1 "2018-08-25T15:37:43Z")

</div>

It seems that string interpolation works differently depending on whether the string consists entirely of a single interpolation:

```julia
struct Foo
    a::String
end

import Base.string
string(F::Foo) = F.a

F = Foo("bar")

"$F" # equals "bar"
"$F$F" # equals Foo("bar")Foo("bar")
"$(F)baz" # equals Foo("bar")baz

```

I have figured out from other threads that I can fix this issue by defining a `show` method for my types, but both the behavior and the solution strike me as unexpected.

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [August 25, 2018, 3:44pm UTC](https://discourse.julialang.org/t/a-string-interpolation-inconsistency/14042/2 "2018-08-25T15:44:06Z")

</div>

Seems like a bug to me. Can you file an issue?

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [August 25, 2018, 3:48pm UTC](https://discourse.julialang.org/t/a-string-interpolation-inconsistency/14042/3 "2018-08-25T15:48:29Z")

</div>

No. It’s not a bug. You are overwriting function you shouldn’t have which hides the implementation. These case are impossible to eliminate for cases where we provide “dispatch functions” (functions that provide user API but dispatches to other overridable functions for custimization).

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [August 25, 2018, 3:56pm UTC](https://discourse.julialang.org/t/a-string-interpolation-inconsistency/14042/4 "2018-08-25T15:56:05Z")

</div>

Ah, I missed that the example was overloading `string` directly and thought it was overloading `show` which is what you should do here.

---

<div class="post-metadata">

### Author: ![sswatson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sswatson/32/5135_2.png) [@sswatson](https://discourse.julialang.org/u/sswatson)
#### Post date: [August 25, 2018, 6:31pm UTC](https://discourse.julialang.org/t/a-string-interpolation-inconsistency/14042/5 "2018-08-25T18:31:29Z")

</div>

Thanks for the clarification. I can say that the reason for my confusion is that the docs include the sentence “Both concatenation and string interpolation call [`string`](https://docs.julialang.org/en/v1.0.0/base/strings/#Base.string) to convert objects into string form.” Given that information, overloading `string` seems like the right thing to do (or at least, it seems like it should work).

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [August 25, 2018, 6:51pm UTC](https://discourse.julialang.org/t/a-string-interpolation-inconsistency/14042/6 "2018-08-25T18:51:27Z")

</div>

I thought so too at first, but once you know how output in [Julia works](https://docs.julialang.org/en/v1/base/io-network/#Multimedia-I/O-1), it makes perfect sense to overwrite `show`. In short, there are different displays offering possibly different rendering of different MIME types with a fallback to plain-text stdout. That’s how for example a Julia Jupyter notebook can just inline display images after loading them, without explicit `show` of the resulting object.

---

<div class="post-metadata">

### Author: ![sswatson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sswatson/32/5135_2.png) [@sswatson](https://discourse.julialang.org/u/sswatson)
#### Post date: [August 25, 2018, 8:14pm UTC](https://discourse.julialang.org/t/a-string-interpolation-inconsistency/14042/7 "2018-08-25T20:14:17Z")

</div>

I’m familiar with the multimedia display system, but I don’t think of it as necessarily intertwined with string interpolation. In any case, there remains the question of how `string(F)*string(F)` and `"$F$F" ` can possibly give different results if the latter is simply calling `string`.

I investigated this just now, and here’s what I found: `"$F"` calls `string` directly, while `"$F$F"` calls `print_to_string`, which in turn calls `print`, which in turn calls `show`. This defaults to `show_default` if no custom `show` method is provided, and that’s where `Foo(3)` comes from.

So I have two conclusions, unless I am overlooking something: (1) it still strikes me as unexpected that `"$F"` is treated differently from `"$F$F"`—though maybe this is unavoidable, and (2) the statement in the documentation about string interpolation calling `string` seems to not be accurate.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [August 26, 2018, 1:17am UTC](https://discourse.julialang.org/t/a-string-interpolation-inconsistency/14042/8 "2018-08-26T01:17:09Z")

</div>

> [@sswatson](#):
>
> it still strikes me as unexpected that `"$F"` is treated differently from `"$F$F"`

It is not. You can just check `code_lowered` or `Meta.@lower`. They are just `string(F)` and `string(F, F)`. Unless you actually want them to return **exactly the same value** (I assume you don’t), this is as similar as you can get to. `string` is the “dispatch” function I mentioned above and no there’s no way around this.

Or basically you are treating them differently by overloading `string` for only one of them, not julia…

> [@sswatson](#):
>
> the statement in the documentation about string interpolation calling `string` seems to not be accurate.

There’s no code bug here. PR to improve and/or correct docs are certainly always welcome.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [August 26, 2018, 1:32am UTC](https://discourse.julialang.org/t/a-string-interpolation-inconsistency/14042/9 "2018-08-26T01:32:28Z")

</div>

Also the doc about “string interpolation calling `string`” is correct that’s exactly what it use, just not on each argument individually. Since it mentions it with concatenation, there should also be little ambiguity but I can see how it could be misleading. Again, PR to improve docs are always welcome.

---

<div class="post-metadata">

### Author: ![sswatson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sswatson/32/5135_2.png) [@sswatson](https://discourse.julialang.org/u/sswatson)
#### Post date: [August 26, 2018, 3:01am UTC](https://discourse.julialang.org/t/a-string-interpolation-inconsistency/14042/10 "2018-08-26T03:01:36Z")

</div>

Ah, that makes perfect sense. `string` is being called in its multi-argument form, and that does _not_ call the single-argument method. The docs are fine, I just managed to misunderstand anyway. Thanks for explaining.

---

<div class="post-metadata">

### Author: ![jeff.bezanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff.bezanson/32/48_2.png) [@jeff.bezanson](https://discourse.julialang.org/u/jeff.bezanson)
#### Post date: [August 27, 2018, 7:34pm UTC](https://discourse.julialang.org/t/a-string-interpolation-inconsistency/14042/11 "2018-08-27T19:34:48Z")

</div>

The help text for `string` says:

```julia
  Create a string from any values, except nothing, using the print function.

```

This is accurate. If you define `print` (which falls back to `show`), that method will be used by string interpolation. This is done so that for many-argument string interpolations (or calls to `string`) the output can be allocated just once: we create an IOBuffer, `print` each argument into it, then turn that into a String.
