# Why string interpolation?

**URL:** <https://forums.swift.org/t/why-string-interpolation/15458>\
**Category:** Discussion\
**Created:** [August 23, 2018, 11:08pm UTC](https://forums.swift.org/t/why-string-interpolation/15458 "2018-08-23T23:08:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fswarbrick](https://avatars.discourse-cdn.com/v4/letter/f/54ee81/32.png) [@fswarbrick](https://forums.swift.org/u/fswarbrick)\
**Post date:** [August 23, 2018, 11:08pm UTC](https://forums.swift.org/t/why-string-interpolation/15458/1 "2018-08-23T23:08:16Z")

</div>

With all this talk of raw strings and string interpolation improvements, I can't help but ask this. Please don't throw stones. Why does Swift have string interpolation at all, rather than say a type of "string concatenation". (Yes, I know Swift supports the + operator for two strings, but I am talking something like this:

```
extension String {
    public static func || (lhs: String, rhs: Any) -> String {
        return lhs + String(describing: rhs)
    }
}

let myField = "test"

let s1 = "Name:\u{0009}myField\nvalue:\t\"\(myField)\""
let s2 = "Name:"||"\u{0009}"||"myField"||"\n"||"value:"||"\t\""||myField||"\""
print(s1)
print(s2)
print("s1 == s2: " + String(s1 == s2))

```

With some new style of "escape characters", such as \u{#####} for Unicode and \{c} for "C-ish style" escape values, s2 could be something like this:

```
"Name:"||\u{0009}||"myField"||\{n}||"value:"||\{t"}||myField||\{"}

```

I personally find this to be more readable, at the expense of being a bit more verbose than string interpolation. With spaces between the concat operator it's even more readable.

```
"Name:" || \u{0009} || "myField" || \{n} || "value:" || \{t"} || myField || \{"}

```

I guess it doesn't truly resolve the need for raw strings, but it just got me wondering.

No, I don't expect Swift to be changed at this point! But I would be interested in the answer to my question above.

---

<div class="post-metadata">

**Author:** ![beccadax](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/beccadax/32/34217_2.png) [@beccadax](https://forums.swift.org/u/beccadax)\
**Post date:** [August 23, 2018, 11:37pm UTC](https://forums.swift.org/t/why-string-interpolation/15458/2 "2018-08-23T23:37:08Z")

</div>

At a technical level, it's important that we recognize interpolations early—_before_ we need to resolve operator overloads—and treat them specially. The type checker slows down and, eventually, errors out if an expression is too complex; if it didn't recognize interpolations and handle them specially, it wouldn't be able to handle more than a handful of interpolations in a single literal.

At a comprehension level, I find the dense punctuation in your example extremely _un_readable. The difference between `"||"`, `"||`, and `||"` is very important, but easy to miss. In your example, the fact that the first "myField" is text but the second is an expression is super-important; how easy is it for you to tell that?

And at an aesthetic level, I find the `s1` line to look much nicer than the `s2` line. It looks like a string literal with some computed parts, not an expression which produces a string. And if you use a multiline string literal (which would work very poorly with your suggestion, by the way), it can be even nicer, an almost perfect picture of what you'll get at runtime:

```swift
let s3 = """
         Name:	myField
         value:	"\(myField)"
         """

```

---

<div class="post-metadata">

**Author:** ![fswarbrick](https://avatars.discourse-cdn.com/v4/letter/f/54ee81/32.png) [@fswarbrick](https://forums.swift.org/u/fswarbrick)\
**Post date:** [August 23, 2018, 11:47pm UTC](https://forums.swift.org/t/why-string-interpolation/15458/3 "2018-08-23T23:47:58Z")

</div>

Thanks Brent.

I'll agree that the first example of s2 is pretty hard to read. I thought of giving an example of what it would be like with a judicious use of space characters, but since it wasn't my "final example" I decided not to. Anyway:

```
let s2 = "Name:" || "\u{0009}" || "myField" || "\n" || "value:" || "\t\"" || myField || "\""

```

To me that's pretty readable. And again, my final example with spaces and "new escapes" even more so (IMO).

Anyhow, your answer makes sense, and I think you for explaining it.

FWIW, my example is a strawman example; I just happened to use tabs and newline because I couldn't think of a better example. (Your solution doesn't use tabs, btw.) But I agree, my idea wouldn't work well (or at all) with multi-line strings. I had not even considered multi-line strings.

I will find the backslashes "smashed" against the literal text hard to read, but I suppose I'll get used to it.  
Thanks again!

---

<div class="post-metadata">

**Author:** ![jawbroken](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@jawbroken](https://forums.swift.org/u/jawbroken)\
**Post date:** [August 24, 2018, 12:20am UTC](https://forums.swift.org/t/why-string-interpolation/15458/4 "2018-08-24T00:20:36Z")

</div>

> [@fswarbrick](#):
>
> (Your solution doesn't use tabs, btw.)

It does, but it uses the tab character directly instead of the `\t` escape.

---

<div class="post-metadata">

**Author:** ![fswarbrick](https://avatars.discourse-cdn.com/v4/letter/f/54ee81/32.png) [@fswarbrick](https://forums.swift.org/u/fswarbrick)\
**Post date:** [August 24, 2018, 12:22am UTC](https://forums.swift.org/t/why-string-interpolation/15458/5 "2018-08-24T00:22:00Z")

</div>

> [@jawbroken](#):
>
> > (Your solution doesn't use tabs, btw.)
> 
> It does, but it uses the tab character directly instead of the `\t` escape.

Very sneaky!
