scala / scala3

The Scala 3 compiler, also known as Dotty.
https://dotty.epfl.ch
Apache License 2.0
5.8k stars 1.04k forks source link

Name clash with Ordering #21345

Open DavidPerezIngeniero opened 1 month ago

DavidPerezIngeniero commented 1 month ago

Compiler version

3.3.3

Minimized code

val x: Ordering[?] = (x: Any, y: Any) => 0

Output

Compilatin error

  |                                      ^
  |Name clash between defined and inherited member:
  |def compare(x: T, y: T): Int in trait Ordering and
  |final def compare(x: Any, y: Any): Int in anonymous class Object with Ordering[Nothing] {...}
  |have the same type after erasure.
  |
  |Consider adding a @targetName annotation to one of the conflicting definitions
  |for disambiguation.
1 error found

Expectation

it conpiles like it does in Scala 2

Other test:

val x: Ordering[?] = new Ordering[?]:
     override def compare(x: Any, y: Any): Int = 0

results in:

 |      object creation impossible, since:
  |      it has 4 unimplemented members.
  |      /** As seen from class $anon, the missing signatures are as follows.
  |       *  For convenience, these are usable as stub implementations.
  |       */
  |        // Members declared in scala.math.Equiv
  |        def equiv(x: T, y: T): Boolean = ???
  |        
  |        // Members declared in scala.math.Ordering
  |        def compare(x: T, y: T): Int = ???
  |        
  |        // Members declared in scala.math.PartialOrdering
  |        def lteq(x: T, y: T): Boolean = ???
  |        def tryCompare(x: T, y: T): Option[Int] = ???
1 error found

Don't know because it complains, because trait Ordering has default implementation for methods like lteq and so on.

Documentation for Ordering.

An Ordering[T] is implemented by specifying compare(a:T, b:T), which decides how to order two instances a and b. Instances of Ordering[T] can be used by things like scala.util.Sorting to sort collections like Array[T].

DavidPerezIngeniero commented 1 month ago

I can define this:

val x: Ordering[Any] = (x: Any, y: Any) =>  0
DavidPerezIngeniero commented 1 month ago

Version 3.4.2 suffers the same problem.