【问题标题】:Why does 2.10 insist on specifying type parameter bounds (worked fine in 2.9)?为什么 2.10 坚持指定类型参数边界(在 2.9 中运行良好)?
【发布时间】:2013-03-23 22:16:45
【问题描述】:

我有以下案例类:

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 说它只需要非通配符类型,这正是我在这里所拥有的。

【问题讨论】:

    标签: scala scala-2.10 scala-2.9


    【解决方案1】:

    似乎有错误说存在类型“_”不限于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])
    

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

    【讨论】:

    • 我已经尝试过第一种方法,它只会导致更多的编译器错误,但第二种方法对我有用。
    • 至于不使用存在类型,你会推荐什么实例?
    • 第一种方法没有任何编译错误——也许您需要包含更多代码才能使它们出现?
    • 对不起,当然。然后我得到:type mismatch; [error] found : Seq[code.notifications.Alert[Product with Serializable with code.notifications.Transport]] [error] required: Seq[code.notifications.Alert[T]] [error] def toAlerts[T &lt;: Transport]: Seq[Alert[T]] = subject.toSeq.flatMap(subject =&gt; {
    • 但是,是的,基本上我尝试跟随链条,我不断收到这些关于Product with Serializable with code.notifications.Transport 不匹配T 的错误,其中T &lt;: Transport
    猜你喜欢
    • 2012-09-19
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-04
    • 2011-05-18
    • 2010-09-16
    相关资源
    最近更新 更多