Skip to content

Remove obsolete note on escapes in interpolations #15450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions docs/_docs/reference/changed-features/interpolation-escapes.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ title: "Escapes in interpolations"
nightlyOf: https://docs.scala-lang.org/scala3/reference/changed-features/interpolation-escapes.html
---

In Scala 2 there is no straightforward way to represent a single quote character `"` in a single quoted interpolation. A `\` character can't be used for that because interpolators themselves decide how to handle escaping, so the parser doesn't know whether the `"` character should be escaped or used as a terminator.
Scala 3 allows `"` (Unicode Quotation Mark) in an interpolation that uses `"` as a delimiter.

In Scala 3, we can use the `$` meta character of interpolations to escape a `"` character. Example:
The quotation mark must be escaped by either `$` (Dollar Sign) or `\` (Reverse Solidus or "backslash").

Note that `\` may also escape `"` in an ordinary string. This behavior is consistent with Scala 2.

```scala
val inventor = "Thomas Edison"
val interpolation = s"as $inventor said: $"The three great essentials to achieve anything worth while are: Hard work, Stick-to-itiveness, and Common sense.$""
val essentials =
s"As $inventor said: $"The three great essentials to achieve anything worthwhile are, first, hard work; second, stick-to-itiveness; third, common sense.$""
val perspiration = "interpolation"
val attribution =
s"As $inventor wrote in a letter in 1927: \"It is quite true I once made the statement that genius is one percent inspiration and 99 percent $perspiration, and I am still of the same opinion.\""
```