Skip to content

SIP-???: Arbitrary idents and $" in interpolations#4663

Closed
som-snytt wants to merge 2 commits intoscala:2.12.xfrom
som-snytt:wip/24-rebase
Closed

SIP-???: Arbitrary idents and $" in interpolations#4663
som-snytt wants to merge 2 commits intoscala:2.12.xfrom
som-snytt:wip/24-rebase

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.

Rebase of #3870

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.
```

SIP-24 After photo, update the test check.
@retronym
Copy link
Member

I would have thought that we'd need to first deprecate trailing underscores in interpolated ids to effect this change in a backwards compatible manner

% scala
Welcome to Scala version 2.11.6 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_51).
Type in expressions to have them evaluated.
Type :help for more information.

scala> {val foo_ = "[foo_]"; val foo_* = "[foo_*]"; s"$foo_*"}
res0: String = [foo_]*

scala> {val foo_ = "[foo_]"; s"$foo_*"}
res1: String = [foo_]*

scala> :quit

% qscala
Welcome to Scala 2.12.0-20150727-215602-f279fb9799 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_51).
Type in expressions for evaluation. Or try :help.

scala> {val foo_ = "[foo_]"; val foo_* = "[foo_*]"; s"$foo_*"}
res0: String = [foo_*]

scala> {val foo_ = "[foo_]"; s"$foo_*"}
<console>:11: error: not found: value foo_*
       {val foo_ = "[foo_]"; s"$foo_*"}
                                ^

I realize this is a contrived example, but we need to spell out any potential compatibility breakages so we can weigh them against the benefits of this proposal.

@som-snytt
Copy link
Contributor Author

Yes, that's the big example of what parses differently. Maybe the community build can explain whether it's worth slowing down the rotation of the earth.

I bet the biggest complainers would be people who had constants "$x_=" where there was no x in scope but only the mutator, and suddenly -Xlint warns about missing interpolator.

Another way of boosting the change: Hey, I spent three days in a Bold Radius fast track to Scala interpolation, and now I'm totally confused about what an identifier is. People want to spend their training dollars efficiently. Some intuitions derive from very ancient history, such as what is an alnum. Now that I've put in my time on Scala idents, I want it to pay dividends.

@retronym
Copy link
Member

Those BR guys are pretty good, I'm sure with a few hours of training (plus some homework) they could teach just about anyone:

Arbitrary scala expressions (and hence identifiers) can be referred to in the general form of interpolation, ${<expr>}. A shorthand version $alnum may be used to refer to alphanumeric identifiers (e.g. s"$foo $Bob1978 $under_score")

The type checker and syntax highlighter will usually help out the people that weren't listening.

@som-snytt
Copy link
Contributor Author

:) Gah! Homework!

Somewhere I noted that Martin slipped in the slip meeting and said ppl are pushing for underscore -- Martin is like my age so I sympathize -- so I think even one extra rule is one too many, etc. Life is too short. (I suspect Martin would agree, as that sentiment receives universal acclaim at a certain point.)

I think you wanted extra help for s"${ $x }" so it's worth adding that the typechecker is totally lacking in empathy.

What the typechecker should really be telling us is how many keystrokes are necessary to make this compile.

And then the preso compiler can offer a quick fix.

@SethTisue SethTisue changed the title SIP-24: Arbitrary idents and $" in interpolations SIP-???: Arbitrary idents and $" in interpolations Aug 20, 2015
@som-snytt
Copy link
Contributor Author

@SethTisue Doesn't that make it throw an exception every time it's referenced?

@lrytz lrytz modified the milestones: 2.12.0-M3, 2.12.0-M4 Oct 5, 2015
@som-snytt
Copy link
Contributor Author

Suspended: scala/slip#3

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