ClaudiuB
05/29/2020, 9:59 PMkotlin.collections.ArrayList.indexOf
. I think it's critical, but maybe not. There are fixes but indexOf
doesn't properly work for me in certain cases ( version 1.3.61)
Premise: val questionsAndAnswers = mutableListOf<Any>()
, in practice containing items of types data class Answer and data class Question. Both have equals(other: Any)
overriden to return true only when other is of the same type.
equals in Answer
override fun equals(other: Any?): Boolean {
return (other as? Answer)?.proof?.sourceNode?.getContentId() == proof?.sourceNode?.getContentId()
}
equals in Question
override fun equals(other: Any?): Boolean {
return (other as? Question)?.item?.getContentId() == item?.getContentId()
}
I do questionsAndAnswers.indexOf(answer)
knowing my answers are only add odd indexes, and I get a 0. I expected either -1 or odd. Check the debugger, i mapped a bunch of data to help troubleshootClaudiuB
05/29/2020, 10:02 PMquestionsAndAnswers.indexOf(answer) => 0
questionsAndAnswers.indexOfFirst { it == answer } => 1
questionsAndAnswers[0] == answer => false
ClaudiuB
05/29/2020, 10:03 PMquestionsAndAnswers.map { it == answer } => size = 3, values are false true false
ClaudiuB
05/29/2020, 10:04 PMquestionAndAnswers.map{it.javaClass}
gives me [Question,Answer,Question]
making the 1st item true impossible. Something is not working rightClaudiuB
05/29/2020, 10:07 PMClaudiuB
05/29/2020, 10:40 PMilya.gorbunov
05/29/2020, 11:39 PMequals
implementation doesn't satisfy one of these contract requirements:
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/equals.htmlilya.gorbunov
05/29/2020, 11:41 PMthis.proof
is null, then the Answer would be equal to anything, which is definitely not symmetric.araqnid
05/30/2020, 4:31 PMother
is a Question, and this.proof
is null, that equals method will return true (as null is equal to null), which appears to match the screenshotted caseClaudiuB
07/22/2020, 8:40 PMClaudiuB
07/22/2020, 8:44 PMI think it's critical, but maybe not.My thinking was wrong 😆 big of me to put a bug happening in my code on the language 🤦♂️
araqnid
07/23/2020, 3:02 PMit looks like when doing different list functions which compare values of different types o1: A and o2: B, there isn’t consistency between which one’s equals function is used.That is exactly correct, which leads to the importance of symmetric equals() implementations. And hence to the difficulty in having data classes extending each other- two siblings in the inheritance tree are unlikely to be able to have symmetric equals() implementations. This issue crops up more with comparator/comparable though in my experience.