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

Pattern match in for comprehensions treats stable identifiers as variables #9324

Closed
scabug opened this issue May 23, 2015 · 4 comments
Closed
Assignees
Labels

Comments

@scabug
Copy link

scabug commented May 23, 2015

This seems related to #900, but a different issue.

I was trying to match on a case object in a for comprehension, but it kept matching all elements. I managed to reduce it to the following test case. It seems that even identifiers starting with a capital letter, or surrounded by back-ticks are treated as free variables. (There is no What identifier defined at all).

scala> case object Test
defined object Test

scala> for (Test <- List(1 ,2, 3)) println(Test)
1
2
3

scala> for (What <- List(1, 2, 3)) println(What)
1
2
3

scala> for (`What` <- List(1, 2, 3)) println(What)
1
2
3

I expected these to behave as in a regular pattern match. I get the same result in 2.11.5, 2.11.6, and 2.12.0-M1.

@scabug
Copy link
Author

scabug commented May 23, 2015

Imported From: https://issues.scala-lang.org/browse/SI-9324?orig=1
Reporter: Rumen Zarev (rumen.zarev)
Affected Versions: 2.11.5, 2.11.6, 2.12.0-M1
See #900

@scabug
Copy link
Author

scabug commented May 23, 2015

@retronym said:
The workaround is:

scala> for (t @ Test <- List(1 ,2, 3)) println(Test)
<console>:9: error: pattern type is incompatible with expected type;
 found   : Test.type
 required: Int
              for (t @ Test <- List(1 ,2, 3)) println(Test)
                       ^

@scabug
Copy link
Author

scabug commented Oct 23, 2015

@som-snytt said:
Is there a label for "has-stackoverflow-question"?

http://stackoverflow.com/q/33298595/1296806

@som-snytt
Copy link

Closing as duplicate of #900 under the rubric "aligns with valdef" as mentioned here.

In brief, "If p is some pattern other than a simple name or a name followed by a colon and a type," then you get pattern-matching behavior for either a val definition or a generator. The generator case falls under refutability, except that, narrowly construed, irrefutability requires a varid.

The alternatively syntaxes (workarounds) are similar in all cases, namely, to change the shape of the pattern.

@SethTisue SethTisue removed this from the Backlog milestone Jan 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants