Skip to content
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

Allow \" in single-quoted interpolated string literals #11751

Merged
merged 1 commit into from Mar 23, 2021

Conversation

lrytz
Copy link
Member

@lrytz lrytz commented Mar 15, 2021

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

Forward-port of scala/scala#8830

`\"` no longer closes single-quoted interpolated string literals.
The escape sequence is not processed by the scanner.
println(s"\"Hello\", $person")
println(s"""\"Hello\", $person""")

println(f"\"Hello\", $person")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this one was wrongly interpreted as \" instead of ". The check file differs with the one in Scala 2.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I filed #11750 about that

println(f"\"Hello\", $person")
println(f"""\"Hello\", $person""")

println(raw"\"Hello\", $person")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Questions: Now that we have $" as way to escape " in any string interpolator, do we really need to special case \" in raw interpolators? Cant we just assume that raw"\" is "\"? How can we end with a \ in a raw now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we really need to special case \" in raw interpolators?

Maybe I misunderstand, but there's no special case or change in the raw interpolator. The change here is that \" no longer closes the string literal, but the interpretation of \" is unchanged (raw"""\"""" and s"""\"""" are the same as before).

In my opinion we should do this change change and fix scala/bug#6476, even if we have $" now. This issue affected a lot of people, and will keep affecting a lot of people in the future, especially beginners (https://stackoverflow.com/questions/21086263/how-to-insert-double-quotes-into-string-with-interpolation-in-scala).

See also the (updated) PR description of scala/scala#8830 (comment).

Cant we just assume that raw"\" is "\"?

I'm not sure what you mean by that.

How can we end with a \ in a raw now?

Using triple-quote. The 2.13 compiler will even tell you that:

scala> raw"c:\"
           ^
       error: unclosed string literal; note that `\"` no longer closes single-quoted interpolated string literals since 2.13.6, you can use a triple-quoted string instead

If desired, I can include this message in Scala 3 as well.

@nicolasstucki nicolasstucki merged commit e59ed69 into scala:master Mar 23, 2021
@dwijnand dwijnand added this to the 3.0.0-RC2 milestone Mar 29, 2021
@Kordyjan Kordyjan modified the milestones: 3.0.0-RC2, 3.0.0 Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants