gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 03:57:50 27 4
gpt4 key购买 nike

我有以下案例类:

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

最佳答案

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

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

关于scala - 为什么 2.10 坚持指定类型参数边界(在 2.9 中工作正常)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15767176/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com