Skip to content

SIP-24: Arbitrary idents and $" in interpolations#3870

Closed
som-snytt wants to merge 1 commit intoscala:2.12.xfrom
som-snytt:wip/24
Closed

SIP-24: Arbitrary idents and $" in interpolations#3870
som-snytt wants to merge 1 commit intoscala:2.12.xfrom
som-snytt:wip/24

Conversation

@som-snytt
Copy link
Contributor

Allow arbitrary identifiers to be interpolated using
simple syntax, including backquoted identifiers.

Identifiers containing a dollar sign must be backquoted.

Accept $" for a literal quote.

scala> val foo = 42; val foo_ = 43; val foo_* = 44
foo: Int = 42
foo_: Int = 43
foo_*: Int = 44

scala> s"$foo_*"
res0: String = 44

scala> s"$`foo`_*"
res1: String = 42_*

scala> val `a..z` = (0 until 26) map ('a' + _) map (_.toChar)
a..z: scala.collection.immutable.IndexedSeq[Char] = Vector(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z)

scala> s"ABCs: $`a..z`"
res2: String = ABCs: Vector(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z)

scala> val foo$bar = 3
foo|: Int = 3

scala> s"<$`foo$bar`"
res3: String = <3

scala> s"$"Don't quote me on that!$" he objected."
res4: String = "Don't quote me on that!" he objected.

Allow arbitrary identifiers to be interpolated using
simple syntax, including backquoted identifiers.

Identifiers containing a dollar sign must be backquoted.

Accept `$"` for a literal quote.

```
scala> val foo = 42; val foo_ = 43; val foo_* = 44
foo: Int = 42
foo_: Int = 43
foo_*: Int = 44

scala> s"$foo_*"
res0: String = 44

scala> s"$`foo`_*"
res1: String = 42_*

scala> val `a..z` = (0 until 26) map ('a' + _) map (_.toChar)
a..z: scala.collection.immutable.IndexedSeq[Char] = Vector(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z)

scala> s"ABCs: $`a..z`"
res2: String = ABCs: Vector(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z)

scala> val foo$bar = 3
foo|: Int = 3

scala> s"<$`foo$bar`"
res3: String = <3

scala> s"$"Don't quote me on that!$" he objected."
res4: String = "Don't quote me on that!" he objected.
```
@retronym
Copy link
Member

retronym commented Jul 9, 2014

It would be nice to include a preceding commit with a neg test to show that these cases were all parse errors before this patch.

@xeno-by
Copy link
Contributor

xeno-by commented Jul 9, 2014

I was always wondering why $" is preferable to \" for a double-quote escape. Could you elaborate on that?

@som-snytt
Copy link
Contributor Author

@xeno-by Maybe I'll elaborate in the SIP document.

@som-snytt
Copy link
Contributor Author

@xeno-by Actually, the SIP already says:

Expression syntax is required because no escape characters are interpreted
when parsing processed string literals:

    scala> s"I coulda been \"${who}body.\""
    <console>:1: error: ';' expected but '.' found.
           s"I coulda been \"${who}body.\""
                                       ^

Imagine a localization macro that converts \"quotes\" to «guillemets».

@soc
Copy link
Contributor

soc commented Jul 9, 2014

Isn't that just adding an additional way to do the same thing?

What's the improvement of e. g.

s"<$`foo$bar`"

over

s"<${foo$bar}"

?

@som-snytt
Copy link
Contributor Author

@soc The point of that example is just that you get s"$anyid" where anyid is any Scala identifier of any ilk, except that $ must be backquoted if you abjure (not to say abhor) braces.

Conversely, though,

`foo$bar`

is permitted in Scala even though it's just another way to write foo$bar.

@xeno-by
Copy link
Contributor

xeno-by commented Jul 9, 2014

@som-snytt I agree that \" is something that will have to be special-cased. However $" is also a special-case, so in that regard these solutions are on par. Maybe there's something else about $" that I'm overlooking?

@som-snytt
Copy link
Contributor Author

@xeno-by I misunderstood you.

f"%2$$s $foo $bar" is not especially fortunate, compared to \$. Similarly, if searching for dollars, r"\$$+" for, let's see, r"\\$+" if it's not necessary to escape the escape.

If \" and \$ were preprocessed, then interpolators would have to special-case them as well. Maybe there are other args against that path.

OTOH, $cala loves dollars. There must be a s"$m$$$$anonfun$i" somewhere in the compiler, right?

In a word, I sympathize but the gist of the discussions weighed against special-cased escapes. In one thread linked from SI-6476, Martin's phrase is "ugly special case." Even though he agrees that the behavior is not intuitive, or intended.

I think a case might be made for guillemets. Then we could discuss how to pronounce guillemets.

@som-snytt
Copy link
Contributor Author

@retronym Not all are parse errs, because the foo_* case consumes more input than previously. That's the breaking change for s"$foo_*".

@retronym
Copy link
Member

I'm going to close this PR. We'll get back to the SIP review process after the summer, at which time the greek tick and calming "all is well" on this PR will be duly noted.

@retronym retronym closed this Jul 18, 2014
@som-snytt
Copy link
Contributor Author

I just needed this. s"Finished $status_?". Just some POC slash test code, but that makes ease of typing even more pleasant. In case anyone @retronym cares.

For the record, also surprising how often I would use ss map s"Done with $_".

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.

5 participants