Adrian Adrian - 1 year ago 77
Scala Question

Signature of sorted method in SeqLike

I don't understand the purpose for the signature of the

method within the

def sorted[B >: A](implicit ord: Ordering[B]): Repr

More precise, I don't get:

  1. The reason for

  2. What does

Perhaps you can shed some light on this.

Thanks in advance for the effort in answering!

Answer Source
  1. The method takes an implicit Ordering which should be able to handle some supertype of A (hence B >: A). I.e. you should be able to use an Ordering[AnyVal] to compare Int values (as AnyVal is a supertype of Int).
  2. Repr is a type parameter of the SeqLike trait itself (SeqLike[+A, +Repr]) described as "the type of the actual collection containing the elements". This is meant to ensure that methods like sorted will return a collection of the same type (e.g. List.sorted will still be a List).