Skip to content

Commit 908fd75

Browse files
Update SIPs state
1 parent 6341350 commit 908fd75

File tree

1 file changed

+132
-132
lines changed

1 file changed

+132
-132
lines changed
+132-132
Original file line numberDiff line numberDiff line change
@@ -1,132 +1,132 @@
1-
---
2-
layout: sip
3-
title: SIP-37 - Quote escapes for interpolations
4-
stage: completed
5-
status: shipped
6-
permalink: /sips/:title.html
7-
redirect_from: /sips/pending/interpolation-quote-escape.html
8-
---
9-
10-
> This proposal has been implemented in Scala 2.13.6 and Scala 3.0.0.
11-
12-
**By: Martijn Hoekstra**
13-
14-
## History
15-
16-
| Date | Version |
17-
|---------------|-----------------------|
18-
| Jul 31th 2018 | Initial Draft |
19-
| Aug 1st 2018 | Process lead comments |
20-
| Nov 2nd 2019 | Link dotty impl |
21-
22-
## Introduction
23-
24-
It's not straight-forward how to have a quote character (`"`) in an
25-
interpolation. Parsing interpolations does not process backslash escapes, but
26-
rather passes the raw string to the interpolator, which then has the option to
27-
process escapes itself as it sees fit. That means there are no lexing rules that
28-
process the escape, and the sequence `\"` simply terminates the interpolation.
29-
30-
Interpolations have a different meta-character -- the `$` character -- which is
31-
treated specially. Interpolations use this escape to splice in arguments, and it
32-
can also be used to escape itself as the sequence `$$` to represent a literal
33-
`$` character.
34-
35-
Because of its special handling during the parse, the `$` could be used to
36-
escape a `"` character to represent a literal `"` withing a string.
37-
38-
## Motivating Example
39-
40-
That the `"` character can't be easily escaped in interpolations has been an
41-
open issue since at least 2012[^1], and how to deal with this issue is a
42-
somewhat common SO question[^2][^3]
43-
44-
{% highlight Scala %}
45-
s"A common question for Scala programmers is "How can I represent a literal " character in Scala interpolations?""
46-
{% endhighlight %}
47-
48-
Doesn't work.
49-
50-
Neither does
51-
52-
{% highlight Scala %}
53-
s"A common question for Scala programmers is \"How can I represent a literal \" character in Scala interpolations?\""
54-
{% endhighlight %}
55-
56-
### Examples
57-
58-
{% highlight Scala %}
59-
s"A common question for Scala programmers is $"How can I represent a literal $" character in Scala interpolations?$""
60-
{% endhighlight %}
61-
62-
### Comparison Examples
63-
64-
There are a number of ways to work around the current restriction.
65-
66-
The simplest is triple-quoting the interpolation:
67-
{% highlight Scala %}
68-
s"""A common question for Scala programmers is "How can I represent a literal " character in Scala interpolations?""""
69-
{% endhighlight %}
70-
71-
Another common workaround is splicing in a separate string in one way or another.
72-
73-
{% highlight Scala %}
74-
//with a normal escape in a string in a block
75-
s"A common question for Scala programmers is ${"\""}How can I represent a literal ${"\""} character in Scala interpolations?${"\""}"
76-
//with a quote character as a block
77-
s"A common question for Scala programmers is ${'"'}How can I represent a literal ${'"'} character in Scala interpolations?${'"'}"
78-
//with an identifier referencing a string that contains a single quote
79-
val quote = "\""
80-
s"A common question for Scala programmers is ${q}How can I represent a literal $q character in Scala interpolations?$q"
81-
{% endhighlight %}
82-
83-
The second set of workarounds is dependent on the actual interpolator, and the
84-
quote becomes an argument. The `s`, `f` and `raw` interpolators splice their
85-
arguments in to the string, as is the obvious use and implementation of an
86-
interpolator. But it's not the only possible use and implementation for an
87-
interpolator and this way of inserting quotes may not work for any given
88-
interpolator.
89-
90-
## Design
91-
92-
This is a non-breaking change. Currently the sequence `$"` within an
93-
interpolation is a syntax error, as has already been noted[^4]
94-
on the original ticket.
95-
96-
## Implementation
97-
98-
The implementation is simple to the point of being trivial: see
99-
the implementation [^5] for the actual change in functionality and the rest of
100-
that PR for the spec and test changes.
101-
102-
There is also an implementation for Dotty.[^7]
103-
104-
## Drawbacks
105-
106-
Adding this feature makes the language just a bit more irregular. There already
107-
is some amount of irregularity around string literals and interpolations in
108-
the language. An argument could be made that this change makes that worse rather
109-
than better.
110-
111-
Because it affects parsing, this change may affect syntax highlighters. Syntax
112-
highlighters tend to already struggle around "funky" strings and interpolations.
113-
114-
## Alternatives
115-
116-
More ambitious proposals around interpolations are possible, and have been
117-
proposed in different forms before. For example, there was a PR thatshows more options
118-
around using `\` as a meta character in interpolations[^6]. It stranded somewhere
119-
between red tape, ambition and changing processes.
120-
121-
I suspect the last word about interpolations hasn't been spoken, and that later
122-
proposals may still make interpolations more regular. This proposal is
123-
deliberately small, and intends not to be in the way of any potential further
124-
proposals.
125-
126-
[^1]: https://github.com/Scala/bug/issues/6476 "\\\" escape does not work with string interpolation"
127-
[^2]: https://stackoverflow.com/questions/31366563/string-interpolation-escaping-quotation-mark/31366588 ""
128-
[^3]: https://stackoverflow.com/questions/17085354/escaping-quotation-marks-in-f-string-interpolation ""
129-
[^4]: https://github.com/scala/bug/issues/6476#issuecomment-292412577 "@retronym said: +1 to s"$"". Because it doesn't compile today, we don't risk changing the meaning of existing programs."
130-
[^5]: https://github.com/Scala/Scala/pull/6953/files#diff-0023b3bfa053fb16603156b785efa7ad ""
131-
[^6]: https://github.com/Scala/Scala/pull/4308 "SI-6476 Accept escaped quotes in interp strings"
132-
[^7]: https://github.com/lampepfl/dotty/pull/7486 "PR in dotty"
1+
---
2+
layout: sip
3+
title: SIP-37 - Quote escapes for interpolations
4+
stage: completed
5+
status: shipped
6+
permalink: /sips/:title.html
7+
redirect_from: /sips/pending/interpolation-quote-escape.html
8+
---
9+
10+
> This proposal has been implemented in Scala 2.13.6 and Scala 3.0.0.
11+
12+
**By: Martijn Hoekstra**
13+
14+
## History
15+
16+
| Date | Version |
17+
|---------------|-----------------------|
18+
| Jul 31th 2018 | Initial Draft |
19+
| Aug 1st 2018 | Process lead comments |
20+
| Nov 2nd 2019 | Link dotty impl |
21+
22+
## Introduction
23+
24+
It's not straight-forward how to have a quote character (`"`) in an
25+
interpolation. Parsing interpolations does not process backslash escapes, but
26+
rather passes the raw string to the interpolator, which then has the option to
27+
process escapes itself as it sees fit. That means there are no lexing rules that
28+
process the escape, and the sequence `\"` simply terminates the interpolation.
29+
30+
Interpolations have a different meta-character -- the `$` character -- which is
31+
treated specially. Interpolations use this escape to splice in arguments, and it
32+
can also be used to escape itself as the sequence `$$` to represent a literal
33+
`$` character.
34+
35+
Because of its special handling during the parse, the `$` could be used to
36+
escape a `"` character to represent a literal `"` withing a string.
37+
38+
## Motivating Example
39+
40+
That the `"` character can't be easily escaped in interpolations has been an
41+
open issue since at least 2012[^1], and how to deal with this issue is a
42+
somewhat common SO question[^2][^3]
43+
44+
{% highlight Scala %}
45+
s"A common question for Scala programmers is "How can I represent a literal " character in Scala interpolations?""
46+
{% endhighlight %}
47+
48+
Doesn't work.
49+
50+
Neither does
51+
52+
{% highlight Scala %}
53+
s"A common question for Scala programmers is \"How can I represent a literal \" character in Scala interpolations?\""
54+
{% endhighlight %}
55+
56+
### Examples
57+
58+
{% highlight Scala %}
59+
s"A common question for Scala programmers is $"How can I represent a literal $" character in Scala interpolations?$""
60+
{% endhighlight %}
61+
62+
### Comparison Examples
63+
64+
There are a number of ways to work around the current restriction.
65+
66+
The simplest is triple-quoting the interpolation:
67+
{% highlight Scala %}
68+
s"""A common question for Scala programmers is "How can I represent a literal " character in Scala interpolations?""""
69+
{% endhighlight %}
70+
71+
Another common workaround is splicing in a separate string in one way or another.
72+
73+
{% highlight Scala %}
74+
//with a normal escape in a string in a block
75+
s"A common question for Scala programmers is ${"\""}How can I represent a literal ${"\""} character in Scala interpolations?${"\""}"
76+
//with a quote character as a block
77+
s"A common question for Scala programmers is ${'"'}How can I represent a literal ${'"'} character in Scala interpolations?${'"'}"
78+
//with an identifier referencing a string that contains a single quote
79+
val quote = "\""
80+
s"A common question for Scala programmers is ${q}How can I represent a literal $q character in Scala interpolations?$q"
81+
{% endhighlight %}
82+
83+
The second set of workarounds is dependent on the actual interpolator, and the
84+
quote becomes an argument. The `s`, `f` and `raw` interpolators splice their
85+
arguments in to the string, as is the obvious use and implementation of an
86+
interpolator. But it's not the only possible use and implementation for an
87+
interpolator and this way of inserting quotes may not work for any given
88+
interpolator.
89+
90+
## Design
91+
92+
This is a non-breaking change. Currently the sequence `$"` within an
93+
interpolation is a syntax error, as has already been noted[^4]
94+
on the original ticket.
95+
96+
## Implementation
97+
98+
The implementation is simple to the point of being trivial: see
99+
the implementation [^5] for the actual change in functionality and the rest of
100+
that PR for the spec and test changes.
101+
102+
There is also an implementation for Dotty.[^7]
103+
104+
## Drawbacks
105+
106+
Adding this feature makes the language just a bit more irregular. There already
107+
is some amount of irregularity around string literals and interpolations in
108+
the language. An argument could be made that this change makes that worse rather
109+
than better.
110+
111+
Because it affects parsing, this change may affect syntax highlighters. Syntax
112+
highlighters tend to already struggle around "funky" strings and interpolations.
113+
114+
## Alternatives
115+
116+
More ambitious proposals around interpolations are possible, and have been
117+
proposed in different forms before. For example, there was a PR thatshows more options
118+
around using `\` as a meta character in interpolations[^6]. It stranded somewhere
119+
between red tape, ambition and changing processes.
120+
121+
I suspect the last word about interpolations hasn't been spoken, and that later
122+
proposals may still make interpolations more regular. This proposal is
123+
deliberately small, and intends not to be in the way of any potential further
124+
proposals.
125+
126+
[^1]: https://github.com/Scala/bug/issues/6476 "\\\" escape does not work with string interpolation"
127+
[^2]: https://stackoverflow.com/questions/31366563/string-interpolation-escaping-quotation-mark/31366588 ""
128+
[^3]: https://stackoverflow.com/questions/17085354/escaping-quotation-marks-in-f-string-interpolation ""
129+
[^4]: https://github.com/scala/bug/issues/6476#issuecomment-292412577 "@retronym said: +1 to s"$"". Because it doesn't compile today, we don't risk changing the meaning of existing programs."
130+
[^5]: https://github.com/Scala/Scala/pull/6953/files#diff-0023b3bfa053fb16603156b785efa7ad ""
131+
[^6]: https://github.com/Scala/Scala/pull/4308 "SI-6476 Accept escaped quotes in interp strings"
132+
[^7]: https://github.com/lampepfl/dotty/pull/7486 "PR in dotty"

0 commit comments

Comments
 (0)