I was trying to be clever with compareTo: override...
# android
t
I was trying to be clever with compareTo: override:
Copy code
override fun compareTo(other: ValveSpan): Int {
    val fields = listOf(ValveSpan::begin, ValveSpan::end, ValveSpan::userTitle)
    val comparisons = fields.asSequence().map { field -> field(this).compareTo(field(other)) }
    return comparisons.firstOrNull { n -> n != 0 } ?: 0
}
Basically an arbitrary number of accessors, work through them lazily until i get to a non zero return and return that. But the compiler complains because:
Copy code
Type mismatch.
Required: {Duration & String}
Found: {Comparable{Duration & String}> & Serializable}
my begin and end accessors do indeed return Durations and the userTitle does indeed return a String. I'm not entirely clear on what the compiler is not happy about here though. it seems like its figuring it out kind of, but not all the way. or there's some stupid syntax error that will only finally embarssingly occur to me finally after pressing send...
e
Copy code
override fun compareTo(other: ValueSpan): Int =
    compareValuesBy(this, other, ValueSpan::begin, ValueSpan::end, ValueSpan::userTitle)
🙏 1
😮 1
c
The reason your version doesn't work might be because Comparable is declared as
<in T>
, so
Comparable<Any>
is not a super type of
Comparable<Duration>
etc.
t
did not know about the compareValuesBy, glad to the use the system variant of exactly what I wanted