Thanks. The equals function looks about what I expected. The hashCode was unexpected, but okay. I assume it’s some prime number magic? Not sure what to do with that.
Maybe this additional information will help with the discussion? I have very little background in Java, so comparisons to Java are not very helpful for me. I’m a C++ programmer, so I can’t help but see things from that perspective. That said, here’s some articles I’ve read on the topic and my commentary.
Introduction to Data Classes in Kotlin by Rajeev Singh Oct 18, 2017
"Although the properties of a data class can be mutable (declared using var), It’s strongly recommended to use immutable properties (declared using val) so as to keep the instances of the data class immutable.
Immutable objects are easier to work with and reason about while working with multi-threaded applications. Since they can not be modified after creation, you don’t need to worry about concurrency issues that arise when multiple threads try to modify an object at the same time."
In my case, I have data where each field can and will be changed independently from the other fields, so making the class immutable seems inadvisable and therefore a data class is inadvisable as well. If I don’t use a data class, I do not get a == operator, or at least I can’t count on the behavior I want. I will need to make my own equals and therefore my own hashCode, even though I will never use the hashCode method.
If that didn’t confuse me enough, this seems to contradict what above description of the == operator does:
Equality in Kotlin (‘==’, ‘===’ and ‘equals’) by Suneet Agrawal Sep 4, 2018
== operator is used to compare the data of two variables.
Please don’t misunderstand this equality operator with the Java == operator as both are different. == operator in Kotlin only compares the data or variables, whereas in Java or other languages == is generally used to compare the references. The negated counterpart of == in Kotlin is != which is used to compare if both the values are not equal to each other.
All I want to do is compare for equivalence. I don’t care about equality. The descriptions of equals just don’t give me a warm fuzzy that the behavior is understood.
At this point, I think I’m better off to delete the == and === not to mention the != and other comparison operators.
Is there a way to do that so if == or any other comparison operator is used, I get an error?
If there is, I can avoid this entire problem.