Skip to content

Commit 826ac10

Browse files
authored
Merge pull request #7486 from martijnhoekstra/sipunquote
escape quote characters in interpolations with $
2 parents 3939747 + 3c33d3c commit 826ac10

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

compiler/src/dotty/tools/dotc/parsing/Scanners.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,7 @@ object Scanners {
10681068
}
10691069
else if (ch == '$') {
10701070
nextRawChar()
1071-
if (ch == '$') {
1071+
if (ch == '$' || ch == '"') {
10721072
putChar(ch)
10731073
nextRawChar()
10741074
getStringPart(multiLine)
@@ -1089,7 +1089,7 @@ object Scanners {
10891089
finishNamed(target = next)
10901090
}
10911091
else
1092-
error("invalid string interpolation: `$$', `$'ident or `$'BlockExpr expected")
1092+
error("invalid string interpolation: `$$', `$\"`, `$'ident or `$'BlockExpr expected")
10931093
}
10941094
else {
10951095
val isUnclosedLiteral = !isUnicodeEscape && (ch == SU || (!multiLine && (ch == CR || ch == LF)))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
layout: doc-page
3+
title: Escapes in interpolations
4+
---
5+
6+
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.
7+
8+
In Dotty, you can use the `$` meta character of interpolations to escape a `"` character.
9+
10+
```scala
11+
val inventor = "Thomas Edison"
12+
val interpolation = s"as $inventor said: $"The three great essentials to achieve anything worth while are: Hard work, Stick-to-itiveness, and Common sense.$""
13+
```

tests/run/t5856.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ object Test extends App {
77
assert(s"$this.##" == "Test.##")
88
assert(s"$this.toString" == "Test.toString")
99
assert(s"$this=THIS" == "Test=THIS")
10+
assert(raw"$"" == "\"")
1011
}

0 commit comments

Comments
 (0)