1

我想实现以下详尽的匹配,但我不知道要删除类型参数,因此无法删除它被删除的警告:

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匹配的主体非常大,我不想添加更多的匹配子句,以便我匹配QLeafand QNode(因为在我的具体情况下,有两个以上的子类,它们也是可变的,因此应该'不是案例类)。解析的类型必须是QNonEmpty[V],不能是QNonEmpty[_]

我可以创建一个QNonEmpty同时匹配QLeaf和的提取器QNode吗?

4

1 回答 1

3

您可以使用存在类型来匹配具有已擦除参数的类型:

type AnyNonEmpty = QNonEmpty[X] forSome { type X }

in match {
  case x: AnyNonEmpty => //...
}

提取器可能如下所示:

object QNonEmpty {
  def unapply(in: QNonEmpty[_]): Option[Any] = Some(in.foo)
}

def test[ V ]( n: Q[ V ]) : String = n match {
   case QEmpty => "empty"
   case QNonEmpty(foo) => foo.toString
}

不过,我认为您不能编写类型安全的提取器,因为在您的示例代码中,该方法的类型参数也被删除了。也许您应该考虑fold为您的 trait 添加一个方法并像这样重写您的示例:

sealed trait Q[ +V ] {
  def fold[A](e: => A, f: V => A): A = e
}

case object QEmpty extends Q[ Nothing ]

sealed trait QNonEmpty[ V ] extends Q[ V ] {
  def foo: V
  override def fold[A](e: => A, f: V => A) = f(foo)
}

// ...

def test[V](n: Q[V]) = n.fold("empty", _.toString)
于 2011-06-07T01:06:12.163 回答