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

Regression: value foreach is not a member of _ #7667

Closed
scabug opened this issue Jul 15, 2013 · 2 comments
Closed

Regression: value foreach is not a member of _ #7667

scabug opened this issue Jul 15, 2013 · 2 comments
Assignees

Comments

@scabug
Copy link

scabug commented Jul 15, 2013

The following code can be compiled with no errors by 2.10.0

Welcome to Scala version 2.10.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_32).
Type in expressions to have them evaluated.
Type :help for more information.

scala>   def packAny(value: Any) {
     |     value match {
     |       case x: collection.GenMapLike[_,_,_]  =>
     |         for((k,v) <- x) { ??? }
     |     }
     |   }
packAny: (value: Any)Unit

But cannot be compiled by 2.10.1 and 2.10.2

Welcome to Scala version 2.10.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_32).
Type in expressions to have them evaluated.
Type :help for more information.

scala>   def packAny(value: Any) {
     |     value match {
     |       case x: collection.GenMapLike[_,_,_]  =>
     |         for((k,v) <- x) { ??? }
     |     }
     |   }
<console>:10: error: value foreach is not a member of _
               for((k,v) <- x) { ??? }
                            ^
@scabug
Copy link
Author

scabug commented Jul 15, 2013

Imported From: https://issues.scala-lang.org/browse/SI-7667?orig=1
Reporter: Denis Petrov (denis)
Affected Versions: 2.10.1
See #6968

@scabug
Copy link
Author

scabug commented Jul 16, 2013

@retronym said:
This is basically a duplicate of #7222.

In 2.10.0, the parser phase of the compiler desugared your for-comprehension as irrefutable, ie to x.foreach { case (k, v) => }.

However, it can't do that in general for tuple patterns; they are supposed to parse to a withFilter call. That's important for code like:

for ( (a, b) <- List[Any]((1, 2), "boo"))

That code should return a one element list; if we omit the withFilter we'll get a MatchError instead.

So now your example is desugared to:

x.withFilter { case (k, v) => (k, v) }.foreach(_ => ???)

{withFilter} returns Repr, the third type argument of GenMapLike, which you have bound to an existential type. As such, it doesn't support foreach.

I suggest in this case to call foreach directly:

x foreach {
  case (k, v) => ???
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants