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
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'
Works as designed, see https://github.com/JetBrains/kotlin-native/blob/master/IMMUTABILITY.md
Perhaps there is a better design that is easier to work with?