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
msort
with multipleOrdering
s for a given type. A type extendingComparable
can only extend it in one way. (Some people consider this a disadvantage.)You can provide an
Ordering
for a type which was implemented without knowledge about it (for example, any Java type!). You can't make an existing type extendComparable
if it doesn't yet.