CLOVIS
04/07/2023, 2:57 PMComparable
from a Comparator
? If there is none, I think it would be a nice addition, to make this possible:
class Foo(…) : Comparable<Foo> by fooComparator.asComparable() {
companion object {
private val fooComparator = compareBy({…}, {…}) // use the stdlib convenience functions for comparators
}
}
Otherwise, it's not possible to reuse these convenience functionsRiccardo Lippolis
04/07/2023, 3:16 PMoverride fun compareTo(other: Foo): Int = fooComparator.compare(this, other)
Regarding delegation, I think the issue here is that the compare
method in the Comparable interface needs access to the instance of Foo
, and I doubt there's a way to access that in a delegate (but maybe I'm mistaken).
If comparing by a single property, something like this works:
class Foo(val x: Int): Comparable<Int> by x
but that won't work when comparing multiple properties 🙂