I have encountered a puzzling issue where a Invali...
# kotlin-native
n
I have encountered a puzzling issue where a InvalidMutabilityException is thrown when a function is invoked on a Object (not to be confused with a class object): ----------------------------------------------------- Uncaught exception from Kotlin's main: kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen org.digieng.gobject_meta.NamespaceInfo@167e938 at ThrowInvalidMutabilityException (0x2406f9) at (0x24f4e6) at kfun:org.digieng.gobject_meta.NamespaceInfo.clearState#internal (0x258a00) at kfun:org.digieng.gobject_meta.NamespaceInfo.load(kotlin.String;kotlin.String) (0x256777) at Konan_start (0x21bac2) at Konan_run_start (0x21b870) at Konan_main (0x21b7a7) at __libc_start_main (0x7f15dd833f45) at (0x21b029) at ((nil)) -----------------------------------------------------
o
Why do you find it puzzling? Top level singleton objects are immutable after creation, so exception is thrown.
n
Was expecting the behaviour of singleton objects on Kotlin Native to be the same as Kotlin JVM/JS. That is mutable properties on Objects (singleton classes) can be changed after being initialised.
Does a class have to be used instead in order to have mutable properties that aren't top level in a source file?
Is there a document that goes into detail on how Kotlin Native handles/approaches immutability?
o
You could use @ThreadLocal annotation to have mutable top level objects
👍 1
k
For a longer explanation on how threading works I wrote a set of blog posts: https://medium.com/@kpgalligan/kotlin-native-stranger-threads-ep-1-1ccccdfe0c99