Skip to content

Java interop : can't compare generic arrays #8615

Closed
@rjolly

Description

@rjolly

minimized code

class ArrayOrdering[N] extends scala.math.Ordering[Array[N]] {
  def compare(x: Array[N], y: Array[N]) = ???
}

Compilation output

  def compare(x: Array[N], y: Array[N]) = ???
      ^
error overriding method compare in trait Comparator of type (x$0: Array[N], x$1: Array[N]): Int;
  method compare of type (x: Array[N], y: Array[N]): Int has incompatible type
(Note that Scala's and Java's representation of this type differs)

expectation

Should work, as in the specialized case:

class ArrayIntOrdering extends scala.math.Ordering[Array[Int]] {
  def compare(x: Array[Int], y: Array[Int]) = ??? // works fine
}

workaround

trait Ordering[T] extends scala.math.Ordering[T] {
  def compare(x: T, y: T) = compares(x, y)
  def compares(x: T, y: T): Int
}
class ArrayOrdering[N] extends Ordering[Array[N]] {
  def compares(x: Array[N], y: Array[N]) = ??? // works fine
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions