Escaping " in single quoted interpolated strings

223 views
Skip to first unread message

Jason Zaugg

unread,
Sep 19, 2013, 12:02:50 PM9/19/13
to scala-i...@googlegroups.com
The discussion in SI-6476 found a consensus that we ought to support an extra $ escape in interpolated strings:

  raw"I dub thee, $"$name$"."

I sort of like the idea.

It's a one line change in our scanner, and a similarly small change to IntelliJ's.

Counter (and counter-counter) arguments:

 - Just use triple quoted strings.
   - The reporter raises the edge case that you can't represent all strings within triple quotes, which is problematic for code generators.
   - It's quite a distraction to switch from single to triple quoted strings as you edit code.
 - What about fixing the syntax highlighting in Sublime/Github/$EDITOR ?
    - I cite Broken Windows: they're already bit broken for interpolated strings, see screenshot below of Sublime, IntelliJ makes the same error).

Thoughts?

-jason


Inline image 1


image.png

Paul Phillips

unread,
Sep 19, 2013, 12:09:58 PM9/19/13
to scala-i...@googlegroups.com

On Thu, Sep 19, 2013 at 9:02 AM, Jason Zaugg <jza...@gmail.com> wrote:
   - It's quite a distraction to switch from single to triple quoted strings as you edit code.

I have a little jingle I sing every time I have to do this. Practice makes perfect: I could fill carnegie hall.

Eugene Burmako

unread,
Sep 19, 2013, 1:04:33 PM9/19/13
to scala-i...@googlegroups.com
Why don't just use \"?


--
You received this message because you are subscribed to the Google Groups "scala-internals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-interna...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Johannes Rudolph

unread,
Sep 19, 2013, 1:13:33 PM9/19/13
to scala-i...@googlegroups.com
On Thu, Sep 19, 2013 at 7:04 PM, Eugene Burmako <xen...@gmail.com> wrote:
> Why don't just use \"?

It's in the ticket: because it would mean that backslash itself would
need to be escaped. Having more than one escape character (at least on
one parsing level) is a path to much confusion.

--
Johannes

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net

Eugene Burmako

unread,
Sep 19, 2013, 1:18:45 PM9/19/13
to scala-i...@googlegroups.com
How about giving a backslash preceding a double-quote special treatment? $" is also a special case, so why not go for an arguably more natural one?


Paul Phillips

unread,
Sep 19, 2013, 2:07:55 PM9/19/13
to scala-i...@googlegroups.com
On Thu, Sep 19, 2013 at 10:18 AM, Eugene Burmako <xen...@gmail.com> wrote:
How about giving a backslash preceding a double-quote special treatment? $" is also a special case, so why not go for an arguably more natural one?

That was what I pleaded for way back in the days before it shipped, but this was deemed not an option for reasons which I couldn't quite understand but which other people seemed to accept.

Johannes Rudolph

unread,
Sep 20, 2013, 4:13:21 AM9/20/13
to scala-i...@googlegroups.com
E.g. for a regexp interpolator that would add a strange special case.
Using a single escape character (i.e. for one parsing level) for all
purposes seems like the more natural solution, no?

Eugene Burmako

unread,
Sep 20, 2013, 4:32:46 AM9/20/13
to <scala-internals@googlegroups.com>
\" in interpolated strings would be a parser-level thing. User-defined interpolators won't have to worry about it.


Johannes Rudolph

unread,
Sep 20, 2013, 4:34:56 AM9/20/13
to scala-i...@googlegroups.com
On Fri, Sep 20, 2013 at 10:32 AM, Eugene Burmako <eugene....@epfl.ch> wrote:
> \" in interpolated strings would be a parser-level thing. User-defined
> interpolators won't have to worry about it.

So what would the meaning of

regexp"[\\"]+" be?

Paul Phillips

unread,
Sep 20, 2013, 5:38:11 AM9/20/13
to scala-i...@googlegroups.com

On Fri, Sep 20, 2013 at 1:13 AM, Johannes Rudolph <johannes...@googlemail.com> wrote:
E.g. for a regexp interpolator that would add a strange special case.
Using a single escape character (i.e. for one parsing level) for all
purposes seems like the more natural solution, no?

Well, sucks for the regex interpolator. "Natural" depends on what you're optimizing for. There may be an infinity of interpolators possible, but most people will only ever use one. And interpolated strings are very often born out of uninterpolated ones, so a hundred times a day I am having to decide whether to go on another formatting adventure by making a string triple-quoted - oh it had embedded \ns, now I'm really screwed - or to commit some crime against aesthetics in the quest to avoid the need for a single quote. In the common activity of printing a List, I'd almost always rather see

  ${args mkString ", "}

than 

  $args

but unless I'm going to start keeping "val commaWithSpace" around or otherwise piling up code to please the quoting mechanism, this is UNREASONABLY difficult to achieve without disruption. Of al the ways which this situation might be characterized, I do not find "natural" to be among them.

Jason Zaugg

unread,
Sep 20, 2013, 6:08:01 AM9/20/13
to scala-i...@googlegroups.com
I don't get your example:

scala> s"  ${Nil mkString ", "}"
res1: String = "  "

-jason

Paul Phillips

unread,
Sep 20, 2013, 6:13:59 AM9/20/13
to scala-i...@googlegroups.com

On Fri, Sep 20, 2013 at 3:08 AM, Jason Zaugg <jza...@gmail.com> wrote:
scala> s"  ${Nil mkString ", "}"

I SWEAR this did not used to work.

I retract everything.


Johannes Rudolph

unread,
Sep 20, 2013, 6:14:06 AM9/20/13
to scala-i...@googlegroups.com
On Fri, Sep 20, 2013 at 11:38 AM, Paul Phillips <pa...@improving.org> wrote:
> On Fri, Sep 20, 2013 at 1:13 AM, Johannes Rudolph
> <johannes...@googlemail.com> wrote:
>>
>> E.g. for a regexp interpolator that would add a strange special case.
>> Using a single escape character (i.e. for one parsing level) for all
>> purposes seems like the more natural solution, no?
>
>
> Well, sucks for the regex interpolator. "Natural" depends on what you're
> optimizing for. There may be an infinity of interpolators possible, but most
> people will only ever use one.

I see this as a consequence of making interpolators general without
allowing hooks into the parser. That said, I understand what you are
saying, there's a tradeoff between a cleaner design (just one
interpolation character) and easier migration (keep \"). Maybe there's
a third possibility: add '\"' as a special case and additionally add
'$"' for homogeneity and then also add '$\' for escaping the backslash
so that you are still able to encode the string backslash-doublequotes
without having to go into full quoted scala expression land.

> And interpolated strings are very often born
> out of uninterpolated ones, so a hundred times a day I am having to decide
> whether to go on another formatting adventure by making a string
> triple-quoted - oh it had embedded \ns, now I'm really screwed - or to
> commit some crime against aesthetics in the quest to avoid the need for a
> single quote. In the common activity of printing a List, I'd almost always
> rather see
>
> ${args mkString ", "}
>
> than
>
> $args

I don't see how this is related to the above but you are preaching to
the choir here. From 5 years ago:

/*
* ObjectFormatter.format("create table #name (#columns[#name #type
#specs(,)*\n](,)*)",t)
* is short for
* "create table" + t.name + "(" +
* t.columns.map(c=> c.name + " " + c.type + " " + c.specs.join(",")).join(",")
* + ")"
*/

https://github.com/jrudolph/bytecode/blob/174b430676c68755b2e37f364d63bdf04e7b6075/EnhancedString/src/net/virtualvoid/string/EnhancedStrings.scala#L14

(I've even got Scala code for this printed on dead trees in my bookshelf...)

Maybe it's time for a revival :)

Johannes Rudolph

unread,
Sep 20, 2013, 6:19:06 AM9/20/13
to scala-i...@googlegroups.com
Ah, I maybe also observed something fishy in a similar constellation
but wasn't able to reproduce the exact thing later on. I think it
happened when I tried to change a string from single- to
triple-quoted. Or maybe it was just \" not working? Can't remember.
Reply all
Reply to author
Forward
0 new messages