为什么我收到“无法找到参数 ord 的隐式值:scala.math.Ordering[T]”?
我有一个简单的示例特征,它具有从 Ordered 派生的某种泛型类型的值。尽管我得到“无法找到参数 ord 的隐式值:scala.math.Ordering[T]”,但我找不到任何实际使用该值的方法。这是代码:
trait Example[T <: Ordered[_]] {
val key: T
def before(that: Example[T]): Boolean = (key < that.key)
}
有什么想法为什么不能编译吗?
I have a simple example trait, which has a value of some generic type descended from Ordered. I can't find any way to actually use the value though as I get "Could not find implicit value for parameter ord: scala.math.Ordering[T]". Here's the code:
trait Example[T <: Ordered[_]] {
val key: T
def before(that: Example[T]): Boolean = (key < that.key)
}
Any ideas why this doesn't compile?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我看来应该是这样
looks to me like it should be
我相信,我不会编译,因为你说
T
应该在任何类型上排序。我们可以编写这样的等效特征(如果我错了,请纠正我 - 我不确定它是否可以被认为是等效的,但至少您会从编译器收到相同的错误):如果您查看
Ordered
特征定义你会发现类似这样的东西:当我们根据这个定义来思考时 - 如果
key
是Ordered[A]
比 < code>that.key 应该是A
类型(根据<
方法签名)。因此编译器实际上无法在Ordered[A]
或换句话说T
上使用它。如果我没有弄错的话,它会尝试找到一些可以用作最后手段的Ordering[T]
(根据伴生对象中的隐式定义)(但它失败了)。因此,如果您像
T <: Ordered[T]
那样定义类型参数T
,它将编译。作为此问题的另一种解决方案,您可以使用上下文绑定,如下例所示:
在这种情况下,实际上会使用隐式转换,因为我们有证据表明
Ordering[T]
存在(特征不能有上下文)或视图绑定,所以我创建了抽象类)这有意义吗?如果您发现我的逻辑有缺陷,请评论! (我会感激这一点)
I believe, I does not compile, because you saying that
T
should be Ordered on any type. We can write an equivalent trait like this (please, correct me, if I'm wrong - I'm not sure whether it can be considered equivalent, but at least you will receive the same error from compiler):If you look in the
Ordered
trait definition you will find something like this:When we will think in terms of this definition - if
key
isOrdered[A]
thanthat.key
should be of typeA
(according to<
method signature). So compiler can't actually use it onOrdered[A]
or in other wordsT
. And if I'm not mistaking it tries to find someOrdering[T]
(according to implicit definition in companion object) that can be used as last resort (but it fails).So if you will define type parameter
T
likeT <: Ordered[T]
it will compile.As another solution to this problem you can use context bound like in this example:
In this case implicit conversion would actually be used because we have evidence, that
Ordering[T]
exists (traits can't have context or view bound, so I created abstract class)Does it makes sense? If you find flaws in my logic - please comment! (I will appreciate this)