Skip to content

escape quote characters in interpolations with $ #7486

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

Merged
merged 1 commit into from
Nov 2, 2019
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/parsing/Scanners.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,7 @@ object Scanners {
}
else if (ch == '$') {
nextRawChar()
if (ch == '$') {
if (ch == '$' || ch == '"') {
putChar(ch)
nextRawChar()
getStringPart(multiLine)
Expand All @@ -1089,7 +1089,7 @@ object Scanners {
finishNamed(target = next)
}
else
error("invalid string interpolation: `$$', `$'ident or `$'BlockExpr expected")
error("invalid string interpolation: `$$', `$\"`, `$'ident or `$'BlockExpr expected")
}
else {
val isUnclosedLiteral = !isUnicodeEscape && (ch == SU || (!multiLine && (ch == CR || ch == LF)))
Expand Down
13 changes: 13 additions & 0 deletions docs/docs/reference/changed-features/interpolation-escapes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
layout: doc-page
title: Escapes in interpolations
---

In Scala 2 there was no straightforward way to represnt 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 or not the " shold be escaped or used as a terminator.

In Dotty, you can use the `$` meta character of interpolations to escape a `"` character.

```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.$""
```
1 change: 1 addition & 0 deletions tests/run/t5856.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ object Test extends App {
assert(s"$this.##" == "Test.##")
assert(s"$this.toString" == "Test.toString")
assert(s"$this=THIS" == "Test=THIS")
assert(raw"$"" == "\"")
}