【发布时间】:2018-03-15 22:08:54
【问题描述】:
我创建了一个小示例程序来尝试找出较大的程序无法编译的原因。
val o1: Ordered[Int] = 1
val o2: Ordered[Int] = 2
println(o1 < o2)
当我将它提供给 scala 时,我得到:
Ordered.scala:3: error: diverging implicit expansion for type scala.math.Ordering[Ordered[Int]]
starting with method ordered in trait LowPriorityOrderingImplicits
println(o1 < o2)
^
one error found
使用“-explaintypes”没有进一步的作用。但是,“-Xlog-implicits”给出以下内容:
math.this.Ordering.comparatorToOrdering is not a valid implicit value for scala.math.Ordering[Ordered[Int]] because:
could not find implicit value for parameter cmp: java.util.Comparator[Ordered[Int]]
scala.this.Predef.conforms is not a valid implicit value for Ordered[Int] => java.lang.Comparable[Ordered[Int]] because:
type mismatch;
found : <:<[Ordered[Int],Ordered[Int]]
required: Ordered[Int] => java.lang.Comparable[Ordered[Int]]
/Users/steshaw/Projects/playground/scala/programming-in-scala/Ordered.scala:3: error: diverging implicit expansion for type scala.math.Ordering[Ordered[Int]]
starting with method ordered in trait LowPriorityOrderingImplicits
println(o1 < o2)
^
math.this.Ordering.comparatorToOrdering is not a valid implicit value for scala.math.Ordering[Ordered[Int]] because:
could not find implicit value for parameter cmp: java.util.Comparator[Ordered[Int]]
scala.this.Predef.conforms is not a valid implicit value for Ordered[Int] => java.lang.Comparable[Ordered[Int]] because:
type mismatch;
found : <:<[Ordered[Int],Ordered[Int]]
required: Ordered[Int] => java.lang.Comparable[Ordered[Int]]
one error found
但这对我没有帮助。想知道此消息的含义以及如何解决?
[更新] 今天与 Scala 2.11.0 相同的代码除了第一个关于“发散隐式扩展”的错误消息外,还会产生第二个错误消息。第二条消息很有帮助。
$ scala Ordered.scala
Ordered.scala:3: error: diverging implicit expansion for type scala.math.Ordering[Ordered[Int]]
starting with method comparatorToOrdering in trait LowPriorityOrderingImplicits
println(o1 < o2)
^
/Users/steshaw/Projects/playground/scala/scalac-errors/Ordered.scala:3: error: type mismatch;
found : Ordered[Int]
required: Int
println(o1 < o2)
^
two errors found
【问题讨论】:
标签: scala