Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SI-8044 Allow binding backquoted varid in patterns #4935

Merged
merged 3 commits into from
May 24, 2016

Conversation

som-snytt
Copy link
Contributor

Previously, a varid could not be backquoted, so that it was not
possible to introduce variables with names such as type in a
match expression.

This commit allows backquoted varids in case x @ _ and
case x: Int. In neither position is a stable id accepted,
that is, an id with leading uppercase.

Therefore, this commit merely relaxes the backquoted varid to
be taken as a normal varid in these contexts.

@scala-jenkins scala-jenkins added this to the 2.12.0-M4 milestone Jan 31, 2016
@lrytz
Copy link
Member

lrytz commented Feb 10, 2016

I'm a little bit worried about this adding another rule for people to remember.
Somebody learning Scala:

  • Ah, nice feature, back-quoted identifiers for things like type or something with spaces
  • Ah, pattern matching has this funny rule: lower case binds, upper case matches
  • Ah, I can match on a lower-case using backticks, so backticks have a different meaning here
  • Ah, in syntactic positions in a pattern where an identifier can only introduce a binding, backticks are back to the other behavior

@retronym
Copy link
Member

The argument for the defence is that it is nice to have a quoting mechanism that works in all cases is nice if you're writing a code generator that targets pattern syntax. (IIRC that was the motivating use case behind the bug report.)

That said, I'm do find the prosecution's arguments persuasive. Reasonable doubt goes the way of the status quo.

@som-snytt WDYT, a dozen days later?

@som-snytt
Copy link
Contributor Author

The defense would highlight on cross-examination that people who are liable to be confused are already confused on day one by case X =>, witness this SO exchange.

Since that is the only context that suffers from weird rules, after they learn about backquoting the varid case x => on day two, they can get their pattern matching certificate and go home happy.

Backquotes have no other special powers, unless you adopt my idea that backquoted underscore introduce a fresh name.

If regularity is a virtue, then why not allow case X @ _? This compromise means varids are still required for binding, so this is less controversial.

Most importantly, it makes possible what is otherwise impossible (and also reasonable). My use case was varids like type and class which are especially common. This will be no argument to anyone who uses spellings such as "tpe" and "defence."

@adriaanm adriaanm self-assigned this Feb 16, 2016
@adriaanm
Copy link
Contributor

We've discussed this during our biweekly world domination meeting, and we find for the case for more backticks. So, we're in favor of your PR, buuuuuut:

  • {case X@p => X} should also be accepted -- is this already the case with this PR?
  • upstream tooling should be notified -- ideally, file an issue to cross-link between scalariform's tracker, intellij, and anything you could think of. We'll make sure to highlight in release notes.
  • we'd also like to make sure @odersky is on board with this.

@adriaanm adriaanm added the release-notes worth highlighting in next release notes label Feb 16, 2016
@som-snytt
Copy link
Contributor Author

Currently it accepts only backquoted varids, only because "varid" is a thing. If anything goes, then I think @retronym 's original suggestion to allow binding X without quotes is valid, and if that's true, can I also bind operators like star, with or without backticks?

I might have just persuaded myself that anything goes.

case Seq(* @ _*) => *
case `X`: Int => X
case C(`X`) => X
case C(`X`: Int) => X

I just ran out of clever things to say. I can imagine the SO questions. I'll go code up the Abide rule for "no backticks in type pattern."

Also, don't tell Dad.

@lrytz
Copy link
Member

lrytz commented Feb 17, 2016

{case X@p => X} should also be accepted -- is this already the case with this PR?

@adriaanm what I meant (@som-snytt points out that Jason mentions it on the ticket): for full regularity 1 match { case X @ 1 => X } should be allowed. And also *.

@som-snytt
Copy link
Contributor Author

I'll add an improvement commit for that soon, and see how it looks.

@som-snytt
Copy link
Contributor Author

Added case * @ _ and case X @ _. Rebased.

@som-snytt
Copy link
Contributor Author

This PR also partially addresses "how do I define multiple vars with non-varid names?"

http://stackoverflow.com/a/12815725/1296806

scala> val (A @ _, B @ _) = (1,2)
A: Int = 1
B: Int = 2

It's not super-great. The fellow on SO had the intuition that backquotes should invert the test for varid:

scala> val (`A`, `B`) = (1,2)

So that didn't error b/c I just defined A and B; it's a no-op match here. But suppose it introduced A and B because of the backquotes.

@som-snytt som-snytt force-pushed the issue/8044-tickvar branch from 0786944 to abc07dd Compare April 26, 2016 20:09
@som-snytt
Copy link
Contributor Author

Rebased just because it caught my attention. Boy, it sure would be nice if it didn't catch my attention again.

som-snytt added 3 commits May 20, 2016 16:12
Previously, a varid could not be backquoted, so that it was not
possible to introduce variables with names such as `type` in a
match expression.

This commit allows backquoted varids in `case x @ _` and
`case x: Int`. In neither position is a stable id accepted,
that is, an id with leading uppercase.

Therefore, this commit merely relaxes the backquoted varid to
be taken as a normal varid in these contexts.
Allows arbitrary identifier in `X @ pat`, including
non-varids. This goes to regularity.

Users of this syntax are not likely to be confused
by the "backquoted var id is stable" rule.

Also for sequence pattern, `X @ _*`.
@som-snytt som-snytt force-pushed the issue/8044-tickvar branch from abc07dd to 1e565d8 Compare May 20, 2016 23:41
@som-snytt
Copy link
Contributor Author

Rebased and added test for

case List(1, `Rest of them` @ _*) => `Rest of them`

That is actually the most common use case for @ syntax in match, as it doesn't have an alternate syntax.

I hope @adriaanm is not on vacation. I guess he accrues both US and Euro vacadays.

@som-snytt
Copy link
Contributor Author

Timeout on docs.comp.

@adriaanm
Copy link
Contributor

/rebuild

oh, ci!

@adriaanm
Copy link
Contributor

LGTM

@adriaanm adriaanm merged commit 808f3d0 into scala:2.12.x May 24, 2016
@som-snytt som-snytt deleted the issue/8044-tickvar branch May 31, 2016 17:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-notes worth highlighting in next release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants