This kotlin-native code:
class MahSingleton {
companion object {
var randomText:String
fun setKex(text:String){
randomText = text
}
}
}
Exporting this code as a iOS Framework and running this:
var singleton: MahSingleton.Companion = MahSingleton.Companion.init()
singleton.setKex(text: "WHAT")
(FYI: Same with running MahSingleton.Companion() instead of using .init() )
Gives
kfun:kotlin.native.concurrent.InvalidMutabilityException.<init>(kotlin.String)kotlin.native.concurrent.InvalidMutabilityException at Freezing.kt
While working just fine if you try to run the same code from Android. Why does it crash on iOS?
Running version '0.9.2' of kotlin-native
This is intended behavior. Singleton objects, including companions are not mutable, see https://github.com/JetBrains/kotlin-native/blob/master/IMMUTABILITY.md. You could mark your companion as @ThreadLocal, although.
@olonho What dependency is required? It does not seem to be included in stdlib. Can't resolve it with either @kotlin.native.ThreadLocal nor @ThreadLocal
No dependencies, it is in stdlib. See - https://github.com/JetBrains/kotlin-native/blob/70e6b711349cf3a5bd937b6fcec124d2ca1244ef/runtime/src/main/kotlin/kotlin/native/Annotations.kt#L49 for declaration and https://github.com/JetBrains/kotlin-native/blob/70e6b711349cf3a5bd937b6fcec124d2ca1244ef/samples/videoplayer/src/main/kotlin/DecoderWorker.kt#L23 for usage example.
@olonho Seems like this is not supported when you try to use it in the common-module doing multiplatform guess the 'correct' way would be to implement a expect/actual setup of it then?
You can create expect typealias being @ThreadLocal on Native and nothing on JVM.
@olonho maybe add this expect/actual realization in stdlib? When use multiplatform with native it's really needed to mark mutability annotations in common code.
Or maybe create multiplatform-additions library with this declaration and other useful...
@Alex009 @olonho Agree 100% this is going to create a lot of issues down the line if its not easily supported from common code
For now work as designed, so closing the original issue.
Most helpful comment
@olonho maybe add this expect/actual realization in stdlib? When use multiplatform with native it's really needed to mark mutability annotations in common code.
Or maybe create multiplatform-additions library with this declaration and other useful...