2

I am using Scala 3.3.7.
When I do

val str1 = raw"\\\" // compile error: unclosed string literal

It is considering the \" as a single character. But when I do

val str1 = raw"\\\\" // compiles

Why does it work when we have even number of backslashes but not odd?

1 Answer 1

5

Enclosing it in triple quotes (multiline) works:

val s = raw"""\\\"""

https://scastie.scala-lang.org/z3YT2nwiTdua7avCIn5YmA

Here is PR which seems to be related to the issue:

\" no longer closes single-quoted interpolated string literals.
The escape sequence is not processed by the scanner.

It seems that this part of the spec is "responsible":

interpolatedString ::= alphaid ‘"’ {[‘\’] interpolatedStringPart | ‘\\’ | ‘\"’} ‘"’
...
Note that the sequence " does not close a normal string literal (enclosed in single quotes).

i.e. \" is treated like two characters - | and " of the literal itself, hence:

val s = raw"\"" // represents \" string

works - https://scastie.scala-lang.org/TM5YTOHfQD2Pel6fHjgjOA

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.