paul paul - 11 months ago 79
Scala Question

Generic type with collection sorted operator

I´m trying to create an implicit class using generic, here the class

object Utils {

implicit class optionUtils(option: Option[Any]) {
def sortedList[T]:List[T] = {
val list:List[T] = option.get.asInstanceOf[List[T]]

And here the invocation


But seems like sorted is not compiling, and the compiler says.

Error:(8, 18) not enough arguments for method sorted: (implicit ord: scala.math.Ordering[T])List[T].
Unspecified value parameter ord.

Any idea how to make it works?.


Answer Source

You have to tell the compiler that T is restricted to an order-able type.

def sortedList[T: Ordering]:List[T] = { ...