Let's consider following trait:
sealed trait AB
case class A(a: Int) extends AB
case class B(b: Int) extends AB
I am trying to collect
to limit a collection to specific subclass.
If I try to collect
, matching individual components and reassembling the tuple:
scala> Seq.empty[(Int, AB)].collect { case (id, a @ A(_)) => (id, a) } : Seq[(Int, A)]
res6: Seq[(Int, ab.A)] = List()
compiler is happy, but if try to return a full match:
scala> Seq.empty[(Int, AB)].collect { case x @ (_, A(_)) => x } : Seq[(Int, A)]
things get ugly:
<console>:27: error: type mismatch;
found : Seq[(Int, ab.AB)]
required: Seq[(Int, ab.A)]
Seq.empty[(Int, AB)].collect { case x @ (_, A(_)) => x } : Seq[(Int, A)]
Why Scala compiler is unable to handle the second case?