Skip to content

Commit

Permalink
Merge pull request #7486 from martijnhoekstra/sipunquote
Browse files Browse the repository at this point in the history
escape quote characters in interpolations with $
  • Loading branch information
odersky committed Nov 2, 2019
2 parents 3939747 + 3c33d3c commit 826ac10
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
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"$"" == "\"")
}

0 comments on commit 826ac10

Please sign in to comment.