次の徹底的な一致を実装したいのですが、型パラメーターを削除する方法がわからないため、型パラメーターが消去されるという警告が表示されます。
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
}
私の具体的なケースでは、一致の本体が非常に大きく、代わりにandcase n
に対して一致するようにこれ以上 match 句を追加したくありません(私の具体的なケースでは 2 つ以上のサブクラスがあり、それらも変更可能であり、したがってケースクラスではありません)。解決された型は である必要がありますが、 ではありません。QLeaf
QNode
QNonEmpty[V]
QNonEmpty[_]
と の両方にQNonEmpty
一致するエクストラクタを作成できますか?QLeaf
QNode