Mi chiedo perché List(3,2,1).toIndexedSeq.sortBy(x=>x)
non funziona:scala - Confondere "divergente implicita espansione" l'errore quando si utilizza "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)
Inoltre, 'Elenco (3,2,1) .toIndexedSeq.sortBy (identità)' dà un errore più utile e 'Elenco (3,2,1). toIndexedSeq [Int] .sortBy (x => x) 'funziona bene. – dhg
Nota che puoi cambiare sortBy e toIndexedSeq: 'List (3, 2, 1) .sortBy (x => x). toIndexedSeq' –