Does kotlin native currently not support hash codes changing throughout the lifecycle of a class for...
t
Does kotlin native currently not support hash codes changing throughout the lifecycle of a class for data classes? Details in 🧵
I have a data class
Copy code
private data class Point(
    var x: Int,
    var y: Int
)
that I am modifying and then inserting into a hashSet. However when I do this I get an error:
Copy code
kotlin.IllegalStateException: This cannot happen with fixed magic multiplier and grow-only hash array. Have object hashCodes changed?
	at kotlin.Throwable#<init>(/opt/buildAgent/work/5f69639f351c4725/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/Throwable.kt:25)
	at kotlin.Exception#<init>(/opt/buildAgent/work/5f69639f351c4725/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/Exceptions.kt:23)
	at kotlin.RuntimeException#<init>(/opt/buildAgent/work/5f69639f351c4725/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/Exceptions.kt:34)
	at kotlin.IllegalStateException#<init>(/opt/buildAgent/work/5f69639f351c4725/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/Exceptions.kt:70)
	at kotlin.collections.HashMap.rehash#internal(/opt/buildAgent/work/5f69639f351c4725/kotlin/libraries/stdlib/native-wasm/src/kotlin/collections/HashMap.kt:239)
	at kotlin.collections.HashMap.ensureCapacity#internal(/opt/buildAgent/work/5f69639f351c4725/kotlin/libraries/stdlib/native-wasm/src/kotlin/collections/HashMap.kt:193)
	at kotlin.collections.HashMap.ensureExtraCapacity#internal(/opt/buildAgent/work/5f69639f351c4725/kotlin/libraries/stdlib/native-wasm/src/kotlin/collections/HashMap.kt:181)
	at kotlin.collections.HashMap#addKey(/opt/buildAgent/work/5f69639f351c4725/kotlin/libraries/stdlib/native-wasm/src/kotlin/collections/HashMap.kt:292)
	at kotlin.collections.HashSet#add(/opt/buildAgent/work/5f69639f351c4725/kotlin/libraries/stdlib/native-wasm/src/kotlin/collections/HashSet.kt:34)
I tried this on jvm and it seems to work fine. Also just copying the object before putting it in the set fixes the issue as well.
Maybe it’s something about inserting the object into the set, mutating the object and then inserting it into the set again
Or wait, it’s probably just because I’m modifying the object after putting into the set. I wonder why the jvm doesn’t have an issue with this. I wonder if it creates a copy.
e
this causes problems on JVM as well, it just doesn't have the same check
https://docs.oracle.com/javase/8/docs/api/java/util/Set.html
Note: Great care must be exercised if mutable objects are used as set elements. The behavior of a set is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is an element in the set.
t
Ahh makes sense. Thank you!!
v
It would be better not to have mutable properties in data class at all. Make changes by copying
copy()
into new instance.
t
I 100% agree with you. I was more just curious about the behavior when it comes to having mutable structures in set.
211 Views