简体   繁体   English

什么是分歧的隐式扩展错误?

[英]What is a diverging implicit expansion error?

While trying to find a solution to another question ( [1] ) I came across a diverging implicit expansion error. 在尝试找到另一个问题( [1] )的解决方案时,我遇到了一个不同的隐式扩展错误。 I'm looking for an explanation about what this means 我正在寻找关于这意味着什么的解释

Here's the use case: 这是用例:

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
                                                ^

If you run this in scala with the -Xlog-implicits argument passed, you get more information: 如果在scala中运行此命令并传递了-Xlog-implicits参数,则可以获得更多信息:

scala.this.Prefed.conforms is not a valid implicit value for (T) => Ordered[T] because: scala.this.Prefed.conforms不是(T)=> Ordered [T]的有效隐含值,因为:

type mismatch: 类型不匹配:

found : <:<[T,T] 发现:<:<[T,T]

required: (T) => Ordered[T] 要求:(T)=>有序[T]

scala.this.predef.conforms is not a valid implicit value for (Ordered[T]) => Ordered[Ordered[T]] because: scala.this.predef.conforms不是(Ordered [T])=> Ordered [Ordered [T]]的有效隐含值,因为:

type mismatch: 类型不匹配:

found : <:<[Ordered[T], Ordered[T]] 发现:<:<[订购[T],订购[T]]

required : (Ordered[T]) => Ordered[Ordered[T]] 要求:(有序[T])=>有序[有序[T]]

math.this.Ordering.ordered is not a valid implicit value for Ordering[T] because: math.this.Ordering.ordered不是Ordering [T]的有效隐含值,因为:

type arguments [T] do not conform to method ordered's type parameter bounds [A <: scala.math.Ordered[A]] 类型参数[T]不符合方法有序的类型参数边界[A <:scala.math.Ordered [A]]

This is mostly speculation, but would seem to make some sense. 这主要是猜测,但似乎有道理。 I will try to investigate further: 我会尝试进一步调查:

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] . 最终, sorted的签名要求它找到Ordering[T]类型的东西。 So it's trying to construct your implicit function ordering . 所以它试图构造你的隐式函数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. 首先,它试图通过找到类型(T) => Ordered[T]的隐式来填充conv ,它在Predef中进行搜索 - 这似乎是在咆哮错误的树。 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 . 然后它试图在同一个地方找到一个隐含的(Ordered[T]) => Ordered[Ordered[T]] ,因为by采用Ordering[S]类型的隐式参数,其中SOrdered[T] conv So it can't construct ordering . 所以它无法构建ordering

It then tries to use ordering in math.Ordering, but this also doesn't fit. 然后它尝试在math.Ordering中使用ordering ,但这也不适合。 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. 如果一个人试图在没有隐式有序函数的情况下定义def foo[T <% Ordered[T]](s : Seq[T]) = s.sorted ,那么它会失败,只有一条好消息说它无法找到适合隐含。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM