I am writing the following code to illustrate the problem:
def max[T <% Ordered[T]](a: T, b: T) = {
val c = a.compare(b)
if (c > 0) a else b
}
def min[T <% Ordering[T]](a: T, b: T) = {
val ord = implicitly[Ordering[T]]
val c = ord.compare(a, b)
if (c > 0) b else a
}
println(s"max is ${max(10, 20)}")
implicit val intOrdering = new Ordering[Int] {
override def compare(x: Int, y: Int): Int = x - y
}
println(s"min is ${min(10, 20)}")
The max
method works well, while the min
method doesn't, complaining No implicit Ordering defined for T
even though I defined the intOrdering
, it still complains.
I would ask why Ordered
works, but Ordering
doesn't here, even if I have provided the implicit definition for the Ordering[Int]