score:6
If you look at the type signature of toIndexedSeq
on List
you'll see it takes a type parameter B
, which can be any supertype of A
:
def toIndexedSeq [B >: A] : IndexedSeq[B]
If you leave out that type parameter then the compiler essentially has to guess what you meant, taking the most specific type possible. You could have meant List(3,2,1).toIndexedSeq[Any]
, which of course can't be sorted since there's no Ordering[Any]
. It seems the compiler doesn't play "guess the type parameter" until the whole expression has been checked for correct typing (maybe someone who knows something about compiler internals can expand on this).
To make it work you can either a) provide the required type parameter yourself i.e.
List(3,2,1).toIndexedSeq[Int].sortBy(x=>x)
or b) separate the expression into two so that the type parameter has to be inferred before calling sortBy
:
val lst = List(3,2,1).toIndexedSeq; lst.sortBy(x=>x)
Edit:
It's probably because sortBy
takes a Function1
argument. The signature of sortBy
is
def sortBy [B] (f: (A) => B)(implicit ord: Ordering[B]): IndexedSeq[A]
whereas sorted
(which you should use instead!) works fine with List(3,2,1).toIndexedSeq.sorted
def sorted [B >: A] (implicit ord: Ordering[B]): IndexedSeq[A]
I'm not sure exactly why Function1
causes this problem and I'm going to bed so can't think about it further...
Source: stackoverflow.com
Related Query
- scala - Confusing "diverging implicit expansion" error when using "sortBy"
- Scala diverging implicit expansion for cats.effect.Timer when using monix TaskApp
- Scala - diverging implicit expansion when using toMap
- Diverging implicit expansion error using SortedSet
- Scala diverging implicit expansion compiler error
- Scala compile server error when using nailgun
- How can I customize Scala ambiguous implicit errors when using shapeless type inequalities
- Why type inference failed in Scala when using implicit conversion on return value of Option.getOrElse?
- Error passing generic values to method using implicit parameter in Scala
- Out of memory Error when using scala to process some xml
- compiler error when using Google guava from scala code
- Compatibility Error When Using Scala SBT with Code Coverage
- Scala implicit ordering error when not specifying type parameters
- Duplicate key error when trying to write an $group aggregation to mongodb from Spark using scala
- Compilation error when using Cascading 2.0 in scala
- Bad class file error when using Scala 2.8.x (2.8.0 and 2.8.1) in Javafx 1.x (1.2 and 1.3.1)
- Spark createStream error when creating a stream to decode byte arrays in IntelliJ using the Scala plugin
- Scala strange error when using foldRight with operator syntax
- Scala Quill error when using infix
- Compiler error when using pattern matching on list of pairs in scala
- Scala throws an error when extract json values using lift web
- Scala diverging implicit expansion
- Scala Future is not properly called when using implicit class
- Why am I getting a " diverging implicit expansion" error when trying to sort instances of an ordered class?
- Why Scala reports error if does not specify this.type when using chain method in inherited class
- When using a scala macro, why the compilation error stacktrace looks so incoherent?
- Regarding org.apache.spark.sql.AnalysisException error when creating a jar file using Scala
- "Need to install JAI Image I/O package." error when using tess4j in IntelliJ IDEA Scala SBT project
- Scala cannot access getter methods when using implicit type-class (variance + polymorphism)
- Error when runnning Scala app using Typesafe Activator
More Query from same tag
- StandardTitanGraph java.lang.Throwable: Hook creation trace error
- A compiling error about scala type inference
- Scala's own LLVM implementation
- Lazy filtering of a range?
- µPickle Couldn't derive type error on write
- Akka: persist to Cassandra and publish to Kafka multiple events
- Grouping list items by comparing them with their neighbors
- How to append List[String] to every row of DataFrame?
- How to auto re-run sbt project in scala
- Pass list of column values to spark dataframe as new column
- Exploding multiple array columns in spark for a changing input schema
- Playframework scala howto create temp variable
- Plural Singletons for class with generic parameters
- remove the filtered line from the file using scala fs2 file streaming
- How to aggregate column values into array after groupBy?
- How to rotate an array using array reversal technique?
- Scala Connect Four Moving Between Rows
- Apache Spark - MLlib - K-Means Input format
- "Infer the types used in a function are literals"?
- error when using fold function in flink
- BufferedStream chaining Scala (or Java)
- Cross-building a project dependent on different versions of specific library
- Counting regex matches in Scala?
- Why do I have to explicitly state Tuple2(a, b) to be able to use Map add in a foldLeft?
- Play Scala - Native access
- Sharing data between nodes using Apache Spark
- Can I change the error message given by the compiler in scala?
- Azure Databricks, could not initialize class org.apache.spark.eventhubs.EventHubsConf
- Automatically update sbt dependencies to latest version
- Not able to apply selectKey on kafka stream