Scala ordering API uses implicit objects. e.g.:
def msort[T](xs: List[T])(implicit ord: Ordering) = { ...}
Java uses Comparable interface for the same purpose.
public static <T extends Comparable<? super T>> void sort(List<T> list) { ... }
Why does Scala prefer implicit types over extending a trait? What are the benefits of implicit parameters?
You can use
msortwith multipleOrderings for a given type. A type extendingComparablecan only extend it in one way. (Some people consider this a disadvantage.)You can provide an
Orderingfor a type which was implemented without knowledge about it (for example, any Java type!). You can't make an existing type extendComparableif it doesn't yet.