Adrian Adrian - 3 months ago 11
Scala Question

Signature of sorted method in SeqLike

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

sorted
method within the
SeqLike
trait:

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


More precise, I don't get:


  1. The reason for
    B>:A

  2. What does
    Repr
    mean?



Perhaps you can shed some light on this.

Thanks in advance for the effort in answering!

Answer
  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).