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

Nested patterns in lambda case #761

Open
jiribenes opened this issue Jan 7, 2025 · 2 comments
Open

Nested patterns in lambda case #761

jiribenes opened this issue Jan 7, 2025 · 2 comments

Comments

@jiribenes
Copy link
Contributor

jiribenes commented Jan 7, 2025

Originally reported by @TheDying0fLight -- I thought there already was an issue for this, but I couldn't find it, sorry!

The following nested pattern matching of a tuple in lambda case is not supported:

[(1, 2), (3, 4)].foldLeft(0) { (acc, (x, y)) => acc + x + y }

as it returns a parser error Expected ; on the => token.

What also doesn't work is using case like Scala would (see comment below):

[(1, 2), (3, 4)].foldLeft(0) { case (acc, (x, y)) => acc + x + y }

as that reports Wrong number of value arguments, given 1, but function expects 2.

A workaround that does actually work is, for example:

[(1, 2), (3, 4)].foldLeft(0) { (acc, tup) => 
  val (x, y) = tup;
  acc + x + y
}

(or just using tup.first, tup.second)

This doesn't show up only for tuples, but for anything nested in general:

record Person(name: String, age: Int)
def main() = 
  [Person("Joe", 42), Person("Jolene", 24)].foldLeft(0) { 
    (acc, Person(name, age)) =>
       acc + age
   }

again returns Expected ; on the =>.

This is somewhat related to #707.

@jiribenes jiribenes changed the title Nested patterns in lambda case are not supported Nested patterns in lambda case Jan 7, 2025
@jiribenes
Copy link
Contributor Author

I think that in Scala 3, one needs the case keyword here:

List((1, 2), (3, 4)).foldLeft(0) { case (acc, (x, y)) => acc + y }

@b-studios
Copy link
Collaborator

b-studios commented Jan 7, 2025

What about the Coq / Roq style?

foo { case x, y => ... } 

which "clearly" matches on two arguments, not a tuple.

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