groostav
10/18/2021, 8:14 PMinline class
to simplify java's`compareTo` function? I was thinking that an inline class
that's wrapping the results would be handy and do a lot for readability.
If not, can we typealias ComparisonResult = Int
and put some docs there?
I feel like kotlin can do a lot to add sugar and/or lipstick to this pig in java.ephemient
10/18/2021, 8:22 PMcompareTo
and indexOf
might be improved by wrappers around the various casesgroostav
10/18/2021, 11:30 PMwhen(left.compareTo(right)){
LEFT_IS_FIRST -> ...
BOTH_EQUAL -> ...
RIGHT_IS_FIRST -> ...
}
I can do that using in LEFT_IS_FIRST_RANGE
where thats Int.Min .. -1
, but thats a couple extra objects and a little math I dont need, and the semantics dont really work out.groostav
10/18/2021, 11:37 PMwhen(left.compareTo(right)){
<= LEFT_IS_FIRST -> ...
EQUAL -> ...
>= RIGHT_IS_FIRST -> ...
}
but, then theres those cryptic <=
and >=
symbols, which betrays the abstraction for its ints.
Alternatively, wouldnt many of these problems be solved with a matches
operator?
//given
typealias ComparisonResult = Int
// i could write
when(left.compareTo(right)){
matches LEFT_IS_FIRST -> ...
matches EQUAL -> ...
matches RIGHT_IS_FIRST -> ...
}
// implemented with
object LEFT_IS_FIRST {
operator fun matches(value: ComparisonResult): Boolean = value <= -1
}
But, again, surely there is a discussion somewhere in a keep/kotl.in issue about custom operators in when
syntaxgroostav
10/18/2021, 11:38 PMobject LEFT_IS_FIRST {
operator fun matches(value: YourType): Boolean {
contract {
//bear with me ive forgotten the exact syntax here
true implies valueNotNull
//tells body of when clause can assume
//the switch val isnt null
false implies RIGHT_IS_FIRST or EQUAL
// give the compiler effective sealaed-class stuff
// such that we dont need an `else` clause when
// all three of these are covered
}
}
}
ephemient
10/19/2021, 12:55 AMwhen (it) { in low..high -> }
is equivalent to if (it >= low && it <= high)
, the range object isn't constructed (the compiler has a few special cases for specific types)ephemient
10/19/2021, 12:57 AMval LEFT_IS_FIRST
)