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 on this fails to compile when type parameter varies by subclass #9394

Open
scabug opened this issue Jul 12, 2015 · 4 comments
Open
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) minimized patmat should compile
Milestone

Comments

@scabug
Copy link

scabug commented Jul 12, 2015

sealed trait Base
case class Up() extends Base
case class Down() extends Base

sealed trait Tracker[B <: Base] {

  def bar = this match {
    case UpTracker() => ???
    case DownTracker() => ???
  }

}
case class UpTracker() extends Tracker[Up]
case class DownTracker() extends Tracker[Down]

produces

PatternMatch.scala:8: error: constructor cannot be instantiated to expected type;
 found   : UpTracker
 required: Tracker[B]
    case UpTracker() => ???
         ^
PatternMatch.scala:9: error: constructor cannot be instantiated to expected type;
 found   : DownTracker
 required: Tracker[B]
    case DownTracker() => ???
         ^
two errors found

This does not occur if we move the method into a separate object:

sealed trait Base
case class Up() extends Base
case class Down() extends Base

sealed trait Tracker[B <: Base]
case class UpTracker() extends Tracker[Up]
case class DownTracker() extends Tracker[Down]

object Foo {
  def bar[B <: Base](t: Tracker[B]) = t match {
    case UpTracker() => ???
    case DownTracker() => ???
  }
}
@scabug
Copy link
Author

scabug commented Jul 12, 2015

Imported From: https://issues.scala-lang.org/browse/SI-9394?orig=1
Reporter: Robin Green (ST_robin_green)
Affected Versions: 2.11.7

@scabug
Copy link
Author

scabug commented Jul 13, 2015

@retronym said:
Another workaround is to modify the original example with:

  def bar = (this: Tracker[_ <: B]) match {
    case UpTracker() => ???
    case DownTracker() => ???
  }

@scabug
Copy link
Author

scabug commented Jul 13, 2015

@retronym said:
Potential fix: scala/scala#4625

@Jasper-M
Copy link
Member

Fixed in dotty.

@SethTisue SethTisue added the fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) label Oct 1, 2020
@SethTisue SethTisue added this to the Backlog milestone Oct 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) minimized patmat should compile
Projects
None yet
Development

No branches or pull requests

5 participants