问题描述
在尝试找到另一个问题的解决方案时()我遇到了一个发散的隐式扩展错误.我正在寻找关于这意味着什么的解释
While trying to find a solution to another question ([1]) I came across a diverging implicit expansion error. I'm looking for an explanation about what this means
这是用例:
scala> implicit def ordering[T](implicit conv: T => Ordered[T], res: Ordering[Ordered[T]]) = Ordering.by(conv)
ordering: [T](implicit conv: (T) => Ordered[T],implicit res: Ordering[Ordered[T]])scala.math.Ordering[T]
scala> def foo[T <% Ordered[T]](s : Seq[T]) = s.sorted
<console>:6: error: diverging implicit expansion for type Ordering[T]
starting with method ordering in object $iw
def foo[T <% Ordered[T]](s : Seq[T]) = s.sorted
^
推荐答案
如果在scala中通过传递-Xlog-implicits
参数来运行它,则会获得更多信息:
If you run this in scala with the -Xlog-implicits
argument passed, you get more information:
类型不匹配:
找到了:<:< [T,T]
found : <:<[T,T]
必填:(T)=>订购[T]
required: (T) => Ordered[T]
scala.this.predef.conforms不是(Ordered [T])=>的有效隐式值. Ordered [Ordered [T]]因为:
scala.this.predef.conforms is not a valid implicit value for (Ordered[T]) => Ordered[Ordered[T]] because:
类型不匹配:
found:<:< [Ordered [T],Ordered [T]]
found : <:<[Ordered[T], Ordered[T]]
required:(Ordered [T])=> Ordered [Ordered [T]]
required : (Ordered[T]) => Ordered[Ordered[T]]
math.this.Ordering.ordered不是Ordering [T]的有效隐式值,因为:
math.this.Ordering.ordered is not a valid implicit value for Ordering[T] because:
类型参数[T]不符合方法订购的类型参数范围[A< ;: scala.math.Ordered [A]]
type arguments [T] do not conform to method ordered's type parameter bounds [A <: scala.math.Ordered[A]]
这主要是猜测,但似乎有些道理.我将尝试进一步调查:
This is mostly speculation, but would seem to make some sense. I will try to investigate further:
这似乎表明这里考虑了三个隐式.最终,sorted
的签名要求它找到类型为Ordering[T]
的东西.因此,它正在尝试构造隐式函数ordering
.首先,它试图通过查找类型为(T) => Ordered[T]
的隐式来填充conv
,在Predef中在其中搜索它-似乎在吠叫错误的树.然后,它尝试在同一位置查找(Ordered[T]) => Ordered[Ordered[T]]
的隐式变量,因为by
采用类型为Ordering[S]
的隐式参数,其中S
通过conv
表示Ordered[T]
.因此它无法构造ordering
.
This seems to suggest that there are three implicits that are being considered here. Ultimately, the signature of sorted
requires it to find something of type Ordering[T]
. So it's trying to construct your implicit function ordering
. Firstly, it's trying to fill in conv
by finding an implicit of type (T) => Ordered[T]
, where it's searching in Predef - which seems like barking up the wrong tree. It's then trying to find an implicit for (Ordered[T]) => Ordered[Ordered[T]]
in the same place, since by
takes an implicit parameter of type Ordering[S]
, where S
is Ordered[T]
by virtue of conv
. So it can't construct ordering
.
然后尝试在math.Ordering中使用ordering
,但这也不适合.但是,我认为这就是令人困惑的发散隐式"信息的原因.问题不在于它们之间存在分歧,而是范围内没有合适的解决方案,而是由于存在两条走下坡路的事实而感到困惑.如果尝试在没有隐式有序函数的情况下定义def foo[T <% Ordered[T]](s : Seq[T]) = s.sorted
,则它会失败,仅显示一条不错的消息,即找不到合适的隐式函数.
It then tries to use ordering
in math.Ordering, but this also doesn't fit. However, I think this is what's giving the somewhat confusing 'diverging implicits' message. The problem isn't that they're diverging, it's that there isn't a suitable one in scope, but it's being confused by the fact that there are two paths to go down. If one tries to define def foo[T <% Ordered[T]](s : Seq[T]) = s.sorted
without the implicit ordered function, then it fails with just a nice message saying that it can't find a suitable implicit.
这篇关于什么是发散的隐式扩展错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!