I don't see a way to do this cleanly with delegation, but reusing the convenience function is still possible by simply implementing the interface like this:
override 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 🙂