Skip to content

Compile error with Java 26 - Ordering inherits conflicting members #769

@cushon

Description

@cushon

This code doesn't compile with the latest builds of JDK 26:

new Ordering[T] {
def compare(x: T, y: T): Int = {
x.asInstanceOf[Comparable[T]].compareTo(y)
}

A recent JDK change JDK-8357219: Provide default methods min(T, T) and max(T, T) in Comparator interface adds default methods named min and max to the Comparator interface, which clash with existing methods inherited in Ordering, and cause the following error:

com/fasterxml/jackson/module/scala/introspect/OrderingLocator.scala:31: error: <$anon: Ordering[T]> inherits conflicting members:
  <defaultmethod> def max[U <: T](o1: U, o2: U): U (defined in trait Comparator) and
  def max[U <: T](x: U, y: U): U (defined in trait Ordering)
  (note: this can be resolved by declaring an `override` in <$anon: Ordering[T]>.);
 other members with override errors are: min
          new Ordering[T] {
              ^
1 error

One possible fix is to explicitly declare min and max methods in the Ordering implementation, with the same behaviour as the conflicting inherited methods:

           new Ordering[T] {
+            override def min[U <: T](x: U, y: U): U = if (compare(x, y) < 0) x else y
+            override def max[U <: T](x: U, y: U): U = if (compare(x, y) > 0) x else y
             def compare(x: T, y: T): Int = {
               x.asInstanceOf[Comparable[T]].compareTo(y)
             }
           }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions