Kotlin-native: Companion Object property change from Swift gives ThrowInvalidMutabilityException at Internal.kt

Created on 17 Oct 2018  路  8Comments  路  Source: JetBrains/kotlin-native

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

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...

All 8 comments

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

@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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

9468305 picture 9468305  路  3Comments

SBNTT picture SBNTT  路  4Comments

jonnyzzz picture jonnyzzz  路  4Comments

nhachicha picture nhachicha  路  4Comments

nvlizlo picture nvlizlo  路  4Comments