The reason this isnât working is that there needs to be some def derived[A]: Ordering[A] = ??? method in the Ordering companion object. Only the name really matters.
Unless you are busy with something else, could you elaborate a bit? I mean, does your observation suggest a remedy for the code in question, a way to remove
given Ordering[RGB] with
def compare(x: RGB, y: RGB): Int = x.ordinal compare y.ordinal
and do something simpler or better instead?
Do you have time to show me the code I could add instead?
If I misunderstand and you are too busy, then thank you anyway for your help.
Itâs not an issue in your code, the standard library needs to be updated to define a derived method on Ordering, but Scala 3 uses the Scala 2.13 standard library which makes this tricky without compiler hacks.
The current status is that we might finally be technically able to add to the Scala 2.13 standard library, or will be very soon. The next step is to decide what we actually need to add and to find qualified people to do it. I opened an issue to track this.
Given this is Scala 3 specific, does it belong to 2.13 library? It seems there is already a library distributed with Scala 3 - at least in my project I see scala3-library_3-3.5.1.jar containing e.g. boundary / break or CanEqual.
This is an extension. Can you please explain why it cannot be added to Scala 3 library? It does not modify any existing class, just extends it by providing what is technically a standalone function, is it not?
It cannot live in the Ordering companion, which would be most natural, but it could live in some new object in scala.util, e.g. scala.util.extensions. That would require an import, like scala.collection.compat.- did, but nothing more.
Yes, but I donât think we want an import. Maybe we can define it as a top-level definition in a file in the Scala package. I have not tried it. Not sure whether it would be workable or desirable. I foresee a lot of challenges with respect to discoverability.
Then we should probably find some other way how to extend the library with things like this (Scala 3 only improvements). I do not like having all Scala 2 classes basically locked for the foreseeable future.
From my point of view this is more important than new language features.Unfortunately I do not see much progress made here since Scala 3 specific stdlib improvements - or even any significant interest in the subject.
My solution to this problem usually is to simply extend java.lang.Enum. Java enums implement Comparable, so the Scala compiler can find an Ordering instance for them.