Hi,
consider the following code:
trait Base
trait Extension
case class A(a: Int) extends Base
case class B(b: String) extends Base
object Main {
type Extend[+T <: Base] = T with Extension
def main(args: Array[String]): Unit = {
val objs = List(new A(8) with Extension , new B("hi") with Extension)
ok(objs)
notOk(objs)
}
def ok(objs: List[Extend[Base]]): Unit = {
objs collect {
case o: A => println(o.a)
case o: B => println(o.b)
}
}
def notOk(objs: List[Extend[Base]]): Unit = {
objs collect {
case A(a) => println(a)
case B(b) => println(b)
}
}
}
Compiling this in Scala 2.12.2 yields:
$ scalac repro.scala
repro.scala:27: error: constructor cannot be instantiated to expected type;
found : A
required: Base with Extension
case A(a) => println(a)
^
repro.scala:28: error: constructor cannot be instantiated to expected type;
found : B
required: Base with Extension
case B(b) => println(b)
^
two errors found
Is this expected behavior? I would have expected a constructor pattern match to be possible in this case.