Hi all! Could you please explain me how to use the...
# kotlin-native
a
Hi all! Could you please explain me how to use the
@ThreadLocal
annotation? I have a code:
Copy code
...
object FooObject {

	@ThreadLocal
	var bar: String = "first"
}

fun testObj() {
	val before = FooObject.bar
	println("before = $before")

	FooObject.bar = "second"

	val after = FooObject.bar
	println("after = $after")
}
...
But when I run it, I receive a error:
Copy code
before = first
Instances of kotlin.Error, kotlin.RuntimeException and subclasses aren't propagated from Kotlin to Objective-C/Swift.
Other exceptions can be propagated as NSError if method has or inherits @Throws annotation.
Uncaught Kotlin exception: kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen org.kotlin.mpp.mobile.FooObject@398928
        at 0   CoreModule                          0x000000010bd9ff36 kfun:kotlin.Exception.<init>(kotlin.String?)kotlin.Exception + 70
        at 1   CoreModule                          0x000000010bd9fea6 kfun:kotlin.RuntimeException.<init>(kotlin.String?)kotlin.RuntimeException + 70
        at 2   CoreModule                          0x000000010bda17a6 kfun:kotlin.native.concurrent.InvalidMutabilityException.<init>(kotlin.String)kotlin.native.concurrent.InvalidMutabilityException + 70
        at 3   CoreModule                          0x000000010bdc6da3 ThrowInvalidMutabilityException + 275
        at 4   CoreModule                          0x000000010bde18f2 MutationCheck + 50
        at 5   CoreModule                          0x000000010bd97c63 kfun:org.kotlin.mpp.mobile.FooObject.<set-bar>(kotlin.String) + 67
        at 6   CoreModule                          0x000000010bd9ed56 kfun:org.kotlin.mpp.mobile.testObj() + 342
        at 7   CoreModule                          0x000000010bd9eb2e kfun:org.kotlin.mpp.mobile.platformName()kotlin.String + 62
        at 8   CoreModule                          0x000000010bd9ea82 
...
Why the
bar
variable is frozen in this case?
y
As
mutation attempt of frozen org.kotlin.mpp.mobile.FooObject@398928
this message shows
FooObject is frozen.
(Kotlin object is frozen by default)
Try like
Copy code
@ThreadLocal
object FooObject {
...
a
Oh, I'm an idiot 🙂 Thank you very much!
👍 1