-
Notifications
You must be signed in to change notification settings - Fork 22
Ordering min/max conflicts with Comparator in Java 26+ #13127
Description
Reproduction steps
Scala version: 2.13.16
object T {
def x = new Ordering[String] {
def compare(x: String, y: String): Int = {
return 0
}
}
}$ $JAVA_HOME/bin/java -version
openjdk version "26-internal" 2026-03-17
$ ./scala-2.13.16/bin/scalac T.scala
T.scala:3: error: <$anon: scala.math.Ordering[String]> inherits conflicting members:
<defaultmethod> def max[U <: String](x$1: U, x$2: U): U (defined in trait Comparator) and
def max[U <: String](x: U, y: U): U (defined in trait Ordering)
(note: this can be resolved by declaring an `override` in <$anon: scala.math.Ordering[String]>.);
other members with override errors are: min
def x = new Ordering[String] {
^
1 error
Problem
The above example doesn't compile with the latest builds of JDK 26, due to the changes in JDK-8357219: Provide default methods min(T, T) and max(T, T) in Comparator interface. The new default methods min and max in the Comparator interface clash with existing methods inherited in Ordering.
I found a few places in real world code where this is causing compilation errors when using the latest JDK versions, e.g. FasterXML/jackson-module-scala#769
Declaring explicit overrides of min / max works around the conflict:
object T {
def x = new Ordering[String] {
+ 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: String, y: String): Int = {
return 0
}
}
}I wonder if it would be possible to reduce the compatibility impact of this change with changes to Ordering? This doesn't seem to reproduce with 2.12.x or 3.x, I'm not sure exactly what the relevant differences are with 2.13.x.