具有集合排序运算符的泛型类型

时间:2016-11-09 16:32:36

标签: scala

我正在尝试使用泛型创建一个隐式类,这里是类

$ echo 'a b c d' |
    awk 'FNR==1{for (i=1;i<=NF;i++) printf "$%d%s", i, (i<NF ? OFS : ORS)} 1'
$1 $2 $3 $4
a b c d

$ echo 'a b c d' |
    awk 'FNR==1{for (i=1;i<=NF;i++) printf "$%d%s", i, (i<NF ? OFS : ORS)} 1' |
    column -t
$1  $2  $3  $4
a   b   c   d

这里是调用

object Utils {

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

但似乎排序不是编译,编译器说。

jsonResponse.get("products").sortedList[String]

知道如何使其有效吗?。

问候。

1 个答案:

答案 0 :(得分:4)

您必须告诉编译器T仅限于可订购类型。

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