Skip to content

Unexpected behavior for inner case class pattern matching #9639

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

Closed
scabug opened this issue Feb 1, 2016 · 5 comments
Closed

Unexpected behavior for inner case class pattern matching #9639

scabug opened this issue Feb 1, 2016 · 5 comments
Milestone

Comments

@scabug
Copy link

scabug commented Feb 1, 2016

I have noticed some unexpected behavior related to inner case class pattern matching. Assume an outer class named Outer and an inner case class named Inner. The generic type of this inner class (e.g. not bound to a particular Outer instance) is Outer#Inner.

Pattern matching on the Inner class works as expected when feeding values of kind Any, while pattern matching on values of kind Outer#Inner does not. More precicely, inner objects belonging to different outer instances are matched successfully, which is unexpected.

The following code illustrates the problem:

class Outer {
  case class Inner(val s: String)

  def belongs1(a: Any): Boolean = {
    a match {
      case Inner(s) => true
      case _        => false
    }
  }

  def belongs2(a: Outer#Inner): Boolean = {
    a match {
      case Inner(s) => true
      case _        => false
    }
  }
}


object Sample extends App {
  val o1 = new Outer
  val o2 = new Outer
  val i2 = o2.Inner("")

  println(o1.belongs1(i2)) // Prints false. This is correct; i2 is inner class of o2
  println(o1.belongs2(i2)) // Prints true. Wrong; i2 belongs to o2, not o1.
}

See discussion on scala-internals group.

@scabug
Copy link
Author

scabug commented Feb 1, 2016

Imported From: https://issues.scala-lang.org/browse/SI-9639?orig=1
Reporter: B. Tommy Jensen (tommy-at-grindvoll.com)
Affected Versions: 2.11.7, 2.12.0-M3
See #6583

@scabug
Copy link
Author

scabug commented Feb 1, 2016

@adriaanm said (edited on Feb 1, 2016 6:20:24 PM UTC):
WIP: https://github.com/adriaanm/scala/tree/t9639

@scabug
Copy link
Author

scabug commented Feb 1, 2016

@paulp said:
See #6583 for a related problem, Test3 in particular.

@scabug
Copy link
Author

scabug commented Feb 1, 2016

@adriaanm said:
Thanks. Jason has been collecting related tickets under the outer-references label, including that one.

@som-snytt
Copy link

This progressed in 2.12. Thanks, Jason.

@SethTisue SethTisue modified the milestones: Backlog, 2.12.0 Apr 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants