user1552545 user1552545 - 4 years ago 119
Scala Question

Why does Scala prefer implicit parameters over extending a trait?

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?

Answer Source
  1. You can use msort with multiple Orderings for a given type. A type extending Comparable can only extend it in one way. (Some people consider this a disadvantage.)

  2. 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 extend Comparable if it doesn't yet.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download