我想实现以下详尽的匹配,但我不知道要删除类型参数,因此无法删除它被删除的警告:
sealed trait Q[+V]
case object QEmpty extends Q[Nothing]
sealed trait QNonEmpty[V] extends Q[V] {
def foo: V
}
final class QLeaf[V](val foo: V) extends QNonEmpty[V]
final class QNode[V](val foo: V, var children: Array[Q[V]]) extends QNonEmpty[V]
def test[V](n: Q[V]): String = n match {
case QEmpty => "empty"
case n: QNonEmpty[V] => n.foo.toString // warning: type parameter V is erased
}
在我的具体情况下,case n
匹配的主体非常大,我不想添加更多的匹配子句,以便我匹配QLeaf
and QNode
(因为在我的具体情况下,有两个以上的子类,它们也是可变的,因此应该'不是案例类)。解析的类型必须是QNonEmpty[V]
,不能是QNonEmpty[_]
。
我可以创建一个QNonEmpty
同时匹配QLeaf
和的提取器QNode
吗?