diverging implicit expansion for type scala.math.Ordering

195 Views Asked by At

I'm sorry for this question but I haven't been able to get an answer from the previous questions. I have a Scala class

case class Problem (
  ref:          Ref,
  v1:      Option[String],
  v2:     Option[Int],
  v3:  Option[Int]
) extends Ordered[Problem] {
   def v = ref.v
   def compare(other: Problem) = {
    import scalaz._
    import Scalaz._

    val v1Comp = ( v1 |@| other.v1 ) { case (r1, r2) => r1.compare(r2)}
    val v2Comp = ( v2 |@| other.2 ) { case (sr1, sr2) => sr1.compare(sr2)}
    val v3Comp = Some( v3.compare(other.v3) )
    (v1Comp, v2Comp, v3Comp) match {
      case (Some(v),_,_,_) if v!=0 => v
      case (_,Some(v),_,_) if v!=0 => v
      case (_,_,Some(v),_) if v!=0 => v
      case _                       => ref.v.compare(other.ref.v)
    }
  }
}

Now in another class I have a statement like:-

val probList = List(problem1, problem2).sorted

But is throws compile time issue:-

diverging implicit expansion for type Option[package.Problem] => Comparable[Option[package.Problem]]

when calling the sorted method.

I do not understand one more thing is that the same code works fine for Scala 2.10.x but upgrading it to 2.11.x is causing this issue.

Any help or guidance will be highly appreciated. Thanks in advance

Note:- the names of class and variable is slightly changed but the structure remains exact same

1

There are 1 best solutions below

3
Dmytro Mitin On

I guess correct is

case class Ref(
                v: Int
              )

case class Problem(
                    ref: Ref,
                    v1: Option[String],
                    v2: Option[Int],
                    v3: Option[Int]
                  ) extends Ordered[Problem] {
  def v = ref.v

  def compare(other: Problem) = {
    import scalaz._
    import Scalaz._

    val v1Comp = (v1 |@| other.v1) { case (r1, r2) => r1.compare(r2) }
    val v2Comp = (v2 |@| other.v2) { case (r1, r2) => r1.compare(r2) }
    val v3Comp = (v3 |@| other.v3) { case (r1, r2) => r1.compare(r2) }
    (v1Comp, v2Comp, v3Comp) match {
      case (Some(v), _, _) if v != 0 => v
      case (_, Some(v), _) if v != 0 => v
      case (_, _, Some(v)) if v != 0 => v
      case _ => ref.v.compare(other.ref.v)
    }
  }
}