Kotlin expression giving different results (runtime vs IDE Evaluate expression)

Issue

val result: Boolean = aList.union(bList).any { it.something?.someOtherFlag == true }

I have two lists I need to join and check if any of the items has some flag set.
This has been giving me some bad results. During debugging:

  1. the data before the line indicates result = true (true should be the right answer)
  2. I get result = false by running the program
  3. directly after the line, when I use the Evaluate expression tool, I get result = true

What is definitely NOT the problem is that there is NO change in the data in between.

Kotlin versions of my app/IDE plugin are different:

Kotlin Plugin: 203-1.5.0
App: 1.4.21

What I am looking for is an explanation: what can be the problem?

Solution

The union method returns a set that will keep only distinct elements, and it might discard different elements when running and when evaluating in debugger. I’m not sure how deterministic it’s supposed to be, but the order could matter.

This could happen if equals() and/or hashCode() for your elements are defined without using something, or if equals()/hashCode() for something‘s class is defined without using someOtherFlag. How are these classes defined?

Note that for data classes, only the properties that are present in the primary constructor are taken into account for the generated equals and hashcode methods.

Answered By – Joffrey

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published