當我不知道爲什麼List(3,2,1).toIndexedSeq.sortBy(x=>x)
不起作用:斯卡拉 - 混淆「隱含分歧擴大」錯誤使用「sortBy」
scala> List(3,2,1).toIndexedSeq.sortBy(x=>x) // Wrong
<console>:8: error: missing parameter type
List(3,2,1).toIndexedSeq.sortBy(x=>x)
^
<console>:8: error: diverging implicit expansion for type scala.math.Ordering[B]
starting with method Tuple9 in object Ordering
List(3,2,1).toIndexedSeq.sortBy(x=>x)
^
scala> Vector(3,2,1).sortBy(x=>x) // OK
res: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3)
scala> Vector(3,2,1).asInstanceOf[IndexedSeq[Int]].sortBy(x=>x) // OK
res: IndexedSeq[Int] = Vector(1, 2, 3)
scala> List(3,2,1).toIndexedSeq.sortBy((x:Int)=>x) // OK
res: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3)
另外,'List(3,2,1).toIndexedSeq.sortBy(identity)'給出了更有用的錯誤和List(3,2,1)。 toIndexedSeq [Int] .sortBy(x => x)'工作得很好。 – dhg 2012-02-29 03:36:55
請注意,您可以切換sortBy和toIndexedSeq:'List(3,2,1).sortBy(x => x)。 toIndexedSeq' – 2012-02-29 15:14:11