Why Kotlin doesn't respect comparable properties in equals() method during bytecode generation for dataclasses?

Another issue is that comparison is less strict than equality. In particular with types that only have a partial order (some elements are neither before or after, but are not the same element either - this can be represented as a directed acyclic graph and is actually quite common). These objects could implement Comparable but would have to use the 0 value to represent the undecided case (as comparison stability is important for users of Comparable). Being ordered in the same position does not require that the items are actually equal, and for partial ordering cannot do so.

If you’re wondering, Comparable is overspecified in that sense and using a less(Than) operator (as C++ does) may be better as for two elements a and b it allows both a < b = false and b<a = false. The downside is that you may not use the equals as if it would be the equivalent to testing both options (it is not in C++).

1 Like