In scala, to make a class comparable we extends Ordered[T]
on the class
case class A(i: Int) extends Ordered[A] {
def compare(that: A): Int = this.i compare that.i
}
and Iteratable[A]
's method sorted
requires implicit parameter of type scala.math.Ordering[A]
to sort Iterable[A]
Seq[A](A(2),A(1)).sorted // Seq(A(1),A(2)) Ordering will provide Ordering[A]
Seq[A](A(2),A(1)).sorted(Ordering[A]) // Seq(A(1),A(2))
and the Ordering
companion object provides an implicit Ordering[T]
for Iterable[A]
My question: How can the Ordering
object can provide Ordering[T]
to sorted
.
Since [T]
will be removed by JVM and it is impossible to know difference between
Ordering[A]
and Ordering[Int]
How come it can work??