Raf
Raf

Reputation: 842

diverging implicit expansion on ordering of polymorphic class

Why am I getting a diverging implicit expansion compiler exception on the following code?

trait Person extends Ordered [Person] {
    def age: Int
    def compare (that: Person) = this.age.compare(that.age)
}

class Dinner[A <: Person](val people: Seq[A]) {
    def who = people.sorted
}


<console>:16: error: diverging implicit expansion for type scala.math.Ordering[A]
starting with method $conforms in object Predef
           def who = people.sorted
                            ^

Ok, I can use people.sorted[Person] but why should I, A is it's subclass.

Upvotes: 2

Views: 292

Answers (1)

Dmytro Mitin
Dmytro Mitin

Reputation: 51683

You specified how to compare Persons but you didn't for A <: Person and in people.sorted compiler has to know how to do that.

Try

trait Person extends Ordered [Person] { 
  def age: Int
  def compare(that: Person): Int = implicitly[Ordering[Person]].compare(this, that)
}

implicit def personOrdering[A <: Person]: Ordering[A] = (x, y) => x.age.compare(y.age)

class Dinner[A <: Person](val people: Seq[A]) {
  def who: Seq[A] = people.sorted
}

case class PersonImpl(age: Int) extends Person
val dinner = new Dinner[PersonImpl](Seq(PersonImpl(30), PersonImpl(20)))
dinner.who // List(PersonImpl(20), PersonImpl(30))

Upvotes: 1

Related Questions