https://kotlinlang.org logo
#compiler
Title
# compiler
g

groostav

09/22/2023, 1:59 AM
Im recently bumping into java's
compareTo
function again, and im reminded of how much i hate it. Any chance kotlin could do some inline class magic to make this disappear? Make a
Comparable
interface that returns an
value class ComparsionResult(val value: Int)
with some nice factory methods
fun leftIsFirst: ComparisonResult = ComparisonResult(-1)
? I cant help but think this is a jvm pimple that kotlin can really elegantly sugar.
e

ephemient

09/22/2023, 2:22 AM
any result < 0 is less and any result > 0 is greater, doesn't have to be specifically -1/0/1, so if you just transparently wrap the results, others can easily write incorrect code
Copy code
if (comparison == leftIsFirst)
which IMO is an API flaw that is almost as bad as the original