Kotlin-native: Singleton shared usage & values modifying @ Swift.

Created on 18 Dec 2018  Â·  3Comments  Â·  Source: JetBrains/kotlin-native

Hi!

First of, thank you for your library.

Setup: Using Kotlin shared code as Konan framework.

Problem: If I'm trying to modify singleton's values at runtime (increment, for example), I'm getting this error:
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

Code example @ Kotlin:

open class AppState {
    val x: Int = 0
    // Singleton @ Android. TODO: Singleton @ iOS.
    companion object {
        val instance = AppState()
    }
////    companion object shared {
////        val instance = AppState()
////    }
    fun incX() {
//     x += 1
        instance.x += 1
    }
}

Also tried using object AppState with same functions, except instance, of course.

Swift code example:

        print(AppStateClass.Companion.init().instance.x)
        AppStateClass.Companion.init().instance.incX()
        print(AppStateClass.Companion.init().instance.x)

So what I'm trying to accomplish (in future) is some kind of Session singleton, which has this field (current X, which can be current room, current chat etc), for example:

var currentX: Room?

and re-assign this variable as user changes current X, like

AppState.Companion.currentX = newValue

All 3 comments

To be clear:

Android usage:
AppStateClass.shared.instance.currentX

if possible with

open class AppStateClass {
    public var x: Int = 0

    companion object shared {
        val instance = AppStateClass()
        fun create(): AppStateClass = AppStateClass()
    }

    fun incX() {
        instance.x += 1
    }

}

but in Swift it's inaccessible.

AppStateClass.shared.init().instance.x — Accessible
        AppStateClass.shared.instance.x — 'No such member'

Perhaps there is a better design that is easier to work with?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

brettwillis picture brettwillis  Â·  4Comments

Cortlandd picture Cortlandd  Â·  4Comments

barsan-md picture barsan-md  Â·  4Comments

SBNTT picture SBNTT  Â·  4Comments

nhachicha picture nhachicha  Â·  4Comments