'Java8 time comparison in Kotlin
I stumbled across some strange behaviour when comparing Java8 time objects. The below does not appear to be valid code.
val t1 = LocalTime.now()
val t2 = LocalTime.now()
val foo: Int = t1 > t2
Yet hovering over the undersquiggled code shows that the overridden function return type is correct:
Any ideas?
Solution 1:[1]
According to the documentation, when using the natively overloaded operators (comparison operators specifically) - Kotlin doesn't just call compareTo but rather performs a compareTo against 0 (thus ending up as a bool).
https://kotlinlang.org/docs/operator-overloading.html#comparison-operators
Comparison operators
Expression Translated to a > ba.compareTo(b) > 0a < ba.compareTo(b) < 0a >= ba.compareTo(b) >= 0a <= ba.compareTo(b) <= 0All comparisons are translated into calls to
compareTo, that is required to returnInt.
The documentation snippet you attached is admittedly a bit confusing.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | SJoshi |

