How do I initialise a variable inside an Object cl...
# kotlin-native
g
How do I initialise a variable inside an Object class that I need before doing requests? For doing the following:
Copy code
AnalyticsConfig.init().baseUrl = "<http://192.168.85.16:8000>"
I get the following error:
Copy code
Uncaught Kotlin exception: kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen com.analytics.core.helper.AnalyticsConfig@94d888
e
If anyone has a better answer i’d love to hear it but the only way I found to work was to make something a
class
rather than an
object
The way the memory graph freezes global objects is pretty brutal
g
No singletons ever because of Kotlin/Native? That sucks 😢
e
I mean, like i said, i hope there’s a better answer than this
g
Hope so too!
e
it’s mostly a problem when you’re modifying hte properties of the singleton
if you have the singleton as a wrapper for some kind of I/O or something it seems like it’s fine, because it’s not modifying its own properties
(although protip, don’t do that with a ktor client as a property because it doesn’t work at all)
o
In your case just set it in constructor of the singleton
g
But constructors are not allowed for objects
☝️ 2
s
g
Interesting! The only thing is that I set up this Object in a common module, so I do not have access to K/N APIs
s
`expect`/`actual` feature is designed to solve this problem too.
g
Yeah but it’s not a value I would like to have hardcorded in the common code or a platform, this baseUrl is something I’d like to set up at runtime from apps using their platform libraries
e
For something similar, I wound up passing in an
interface
o
I am talking
init
block
e
i think you might still have the problem of mutating something frozen if you hang on to the object
o
Or indeed as Slava suggested, use atomic reference
e
🔥 1
g
Yeah I am trying to avoid to have to send what I need configured once in every function
I am porting this analytics feature from an App to KMP and wanted to really have 1-1 same API
e
Yeah…I think i tried just hanging on to the object but was still getting the frozen thing. @olonho are you suggesting something like:
Copy code
object Thing {
   init { // set up here }
}
if so, how would you pass in the information you’re trying to set up?
☝️ 1
o
in case above it’s just calling top-level function, and see https://github.com/JetBrains/kotlin-native/blob/master/samples/tetris/src/tetrisMain/kotlin/Config.kt#L11 for solving similar problem in generic way
g
hmmm… I have a hard time understanding how to pass a value to a Singleton written in the commonMain sourceset and how to use it from Kotlin/Native mainly (it is trivial to use in JVM and JS)…
If only Objects had constructors… 🙏
e
the Extremely Stupid Workaround part of my brain says “Make a wrapper class in your
iosMain
which can be instantiated and have that pass the value from Swift”
have not tried this though so i cannot reasonably recommend it
g
There’s this, not sure it is applicable to KMP thought https://medium.com/@BladeCoder/kotlin-singletons-with-argument-194ef06edd9e
Plus this is made for companion objects, not pure Objects
We could pass a lambda which returns our configured value (in my case a String) in the same fashion, I think. Going to try that -> Doesn’t work.
😢 1