Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

scala - Why am I getting a " diverging implicit expansion" error when trying to sort instances of an ordered class?

There are a lot of questions on the subject but after one hour of reading, I still cannot understand what I am doing wrong. Here is a minimal example of the code I have (Scala 2.11):

object Anomalies {

    sealed abstract class AnomalyType(val priority: Int) 
                    extends Ordered[AnomalyType] {
        override def compare(that: AnomalyType): Int =
            this.priority - that.priority
        def name = toString()
    }

    case object CRITICAL extends AnomalyType(0)
    case object SERIOUS extends AnomalyType(1)
    case object WARN extends AnomalyType(2)
}

When I try a basic comparison, it works fine:

scala> Anomalies.CRITICAL < Anomalies.WARN
res17: Boolean = true

Yet, when trying to order a list, the compiler yells at me:

scala> Seq(Anomalies.CRITICAL, Anomalies.WARN).max
<console>:26: error: diverging implicit expansion for type
    Ordering[Anomalies.AnomalyType with Product with Serializable]
    starting with method $conforms in object Predef
        Seq(Anomalies.CRITICAL, Anomalies.WARN).max

EDIT: Thanks to Dmytro Mitin I now know that Seq[Anomalies.AnomalyType](Anomalies.CRITICAL, Anomalies.WARN).max and Seq(Anomalies.CRITICAL, Anomalies.WARN).max[Anomalies.AnomalyType] are working.

Can anyone help me understand the error and why the specifying the type fixes it? Is there a way not to have to specify it explicitly?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It's enough to specify type parameter:

Seq[Anomalies.AnomalyType](Anomalies.CRITICAL, Anomalies.WARN).max // WARN

or

Seq(Anomalies.CRITICAL, Anomalies.WARN).max[Anomalies.AnomalyType] // WARN

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...