Closed
Description
minimized code
def sort[A](as: List[A])(given o: Ordering[A]): List[A] = {
def insert(list: List[A], elem: A): List[A] =
list match {
case Nil => elem :: Nil
case head :: tail =>
if o.lt(elem, head) then elem :: head :: tail
else head :: insert(tail, elem)
}
as.foldLeft(Nil)(insert)
}
case class Rational(num: Int, denom: Int)
given Ordering[Rational] {
def compare(x: Rational, y: Rational): Int =
x.num * y.denom - x.denom * y.num
}
sort(List(Rational(1, 2), Rational(1, 3))) // java.lang.VerifyError: Bad operand type when invoking <init>
expectation
The compiled code should not throw a run-time exception.
Note that calling the sorted
method on List
does not produce the problem, and the same code pasted in a REPL does not produce the problem either.