Skip to content

Unable to use a given instance from a worksheet #7463

Closed
@julienrf

Description

@julienrf

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions