Pre SIP/mini SIP $" escapes in interpolations


This is a pre SIP to suggest allowing the escape sequence $" for a " character in interpolations.




It’s rather cumbersome to write a single quote character in an interpolation. That’s annoying. This proposal fixes that.

  • Why use $ as an escape character?

Because that’s the only character that’s significant to the parse of an interpolation. Since the " normally terminates the parse, but “normal” escapes get processed by the interpolator rather than the parser, only $ usually influences the initial parse.

In addition, this can’t break anyone’s code, since $" in an interpolation right now is a parse error.

  • Why not use \ as an escape character?

Because the interpolator is responsible for processing escapes, it would be inconsistent for the parser to process the \ only as an escape for a preceding ". Various proposals around this have made the rounds, and it has been found very controversial at least. The implementation also seems somewhat hairy.

  • Could this be part of something more ambitious?

Yes. I don’t have that ambition though. Of all the proposals around interpolations and escapes I’ve read so far, this proposal breaks none of them.


While I don’t want to suggest that the size of a diff is indicative if the importance of a change, or that small changes can’t have far-reaching impact, I want to stress that the actual implementation of this change is 2 lines of code changed. Not long ones either.


Thanks Martijn for this suggestion :+1: I like the idea, I always get escape characters wrong and I don’t like using triple quotes to fix this kind of issues. Let’s see what the Community thinks, and then we can proceed with a SIP + an official discussion in one of our SIP meetings.


What’s wrong with s"${Quote}"?

Welcome to Scala 2.12.5 (GraalVM 1.0.0-rc2, Java 1.8.0_171).
Type in expressions for evaluation. Or try :help.

scala> val Quote = '"'
Quote: Char = "

scala> s"$Quote"
res0: String = "


How is that better than ${"’"}


The other quote? In any case, either the typo or my inability to read small font demonstrates the problem, that escaped quote should be easy to write and read. Arguably, even backslash quote isn’t obvious, even if it were supported.


It saves a declaration and a bunch of characters


First, that inserts a ' single quote, not a " double quote. Second, while it is not much of a problem in a monospace font, ${'"'}, ${’"’} is hard to decipher without leaning in and thus intrrupting the normal reading of it all.

And thirdly, $" would be a simple rule to learn but be concise and unambiguous. ${'"'} brings in the expanded format of $, uses a character in a string environment which may be confusing (and using ${"\""} may relieve that but then you’re thinking “I’m inserting a string with an escaped quote because I cannot just escape the quote normally?”), and ultimately also adds a small overhead in that now the string array passed to the interpolator is one entry longer and one more run-time insertion is needed.

At least, that’d be my gripes with favoring $" over it.


I’m in favor of the proposal, I was responding to @yangbo’s s"$Quote"


Oh gosh, my bad then. Sorry!


@jvican we’re approaching the point where you should decide whether or not this suggestion is at an appropriate state to be submitted as a SIP, or that it needs more work. To help you evaluate that, if I were to submit this as a SIP, I would do that with this branch:


Hey Martijn, I think this merits a proposal and the branch you linked LGTM :smile: I’m on vacation but I will get back to your proposal as soon as I’m back (by the way, I commented on the commit of your PR several suggestions).