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

Issue with extractor pattern (unapply override with different arity) #13008

Open
akshayakp97 opened this issue Jun 13, 2024 · 2 comments
Open
Labels
Milestone

Comments

@akshayakp97
Copy link

Reproduction steps

Scala version: 2.13

// Step 1: Define the case class Person
case class Person(name: String, age: Int, id: Int)

// Step 2: Define the companion object with the unapply method
object Person {
  def unapply(person: Person): Option[(String, Int)] = {
    Some((person.name, person.age))
  }
}

// Define an object
val person = Person("Alice", 30, 12345)

// Extract the fields
val canBind = person match {
  case person @ Person(name, age, id) => true
  case _ => false
}

println(s"$canBind")

Problem

The above code works fine in Scala 2.12. However, it doesn't compile with scala 2.13. With scala 2.13, I see error: too many patterns for object Person offering (String, Int): expected 2, found 3.

Discord conversation link

@lrytz
Copy link
Member

lrytz commented Jun 13, 2024

Scala 3 also errors:

-- Error: /Users/luc/code/scala/scala13/sandbox/U.scala:17:25 ------------------
17 |    case person @ Person(name, age, id) => true
   |                         ^
   |this case is unreachable since type (String, Int) is not a subclass of class Tuple3
1 error found

@som-snytt
Copy link

I see I quoted scripture on chat:

However, instead of a case class, the stable identifier xx denotes an object which has a member method named unapply or unapplySeq that matches the pattern.

It's not clear whether the case class can participate in constructor patterns when the unapply does not match the pattern.

@SethTisue SethTisue added this to the Backlog milestone Nov 6, 2024
@SethTisue SethTisue changed the title Issue with extractor pattern in Scala 2.13 Issue with extractor pattern (unapply override with different parity) Nov 6, 2024
@SethTisue SethTisue changed the title Issue with extractor pattern (unapply override with different parity) Issue with extractor pattern (unapply override with different arity) Nov 6, 2024
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