为什么2.10坚持指定类型参数边界(在2.9中正常工作)?

时间:2013-04-02 14:47:41

标签: scala scala-2.10 scala-2.9

我有以下案例类:

case class Alert[T <: Transport](destination: Destination[T], message: Message[T])

在Scala 2.9.2中,以下方法签名编译良好:

def send(notification: Alert[_]) {
  notification match {
    ...
  }
}

现在在Scala 2.10.1中,它无法编译并出现以下错误:

type arguments [_$1] do not conform to class Alert's type parameter bounds [T <: code.notifications.Transport]

这是为什么?我该如何修复错误?简单地给send提供相同类型的边界会导致更多的编译错误......

更新:查看SIP-18,我认为原因是我没有启用存在类型,因为SIP-18说它只需要非通配符类型,这正是我在这里所拥有的。

1 个答案:

答案 0 :(得分:2)

似乎有错误说存在类型“_”不限于Transport的子类型。这可能是首选的解决方案,

trait Transport
trait Destination[T]
trait Message[T]
case class Alert[T <: Transport](destination: Destination[T], message: Message[T])

def send[T <: Transport](notification: Alert[T]) {
  notification match {
    case _ => ()
  }
}

这似乎也有效,

def send(notification: Alert[_ <: Transport])

但我认为最好不要使用存在类型。