I want to make my case class Event[K, V]
be ordered by key K
always. But I need to be able to compare Events of different values V
. How can I solve this diverging implicit expansion?
import scala.math.Ordering
object Event {
case class Event[K, V](key: K, value: V)
(implicit o: Ordering[K]) extends Ordered[Event[K, _]] {
override def compare(that: Event[K, _]): Int = o.compare(key, that.key)
}
}
object Main extends App {
// mimicking a librarys function
def lala[O](e: O)(implicit ordering: Ordering[O]) = ???
val b = Event.Event("a", 12) <= Event.Event("a", 11.99)
lala(Event.Event("a", 12))
}
The call to lala
does not compile because of this diverging implicit expansion:
diverging implicit expansion for type
scala.math.Ordering[Event.Event[String,Int]] starting with method $conforms
in object Predef lala(Event.Event("a", 12))