The following is a quick-sort function written in Scala to sort a list of mixed types(int, double, float etc.). The error popped out and said in line 3 "Type mismatch, expected: T => Boolean, actual: T => Any Cannot resolve symbol <". How do I fix this?
The Intellij IDE running on Windows 10 gave this error message.
def qsort[T](list: List[T]): List[T] = list match {
case Nil => Nil
case pivot :: tail =>
val(smaller, rest) = tail.partition(_ < pivot)
qsort(smaller) ::: pivot :: qsort(rest)
}
T
needs to be aComparable
type if you want to compare it. – Carcigenicate<
etc. when you call the generic function, but Scala does this check when it compiles the function. Scala needs to be sure it will work for all possibleT
, so you need to restrict the allowable types somehow. You can restrict the type itself, or add animplicit
parameter. – Tim