Can some help me in suggesting how to create a fro...
# kotlin-native
p
Can some help me in suggesting how to create a frozen singleton that takes in init parameters?
Copy code
actual open class SomeClass private constructor(
        private val someProperty: SomeProperty
) {

    actual companion object {
        private var someClass: SomeClass? = null

        fun initialize(someProperty: SomeProperty){
            someClass = SomeClass(someProperty)
        }

        actual fun getInstance(): SomeClass {
            if (someClass == null) {
                throw UninitializedPropertyAccessException("SomeClass is not initialised yet")
            }
            return someClass as SomeClass
        }
    }
}
This won't work because companion object is immutable and we cant change someClass property in there. Is there any way to get a Frozen Singleton with params?
o
problem is at which moment you want it to be called, and when make it available. But if you'll change
Copy code
someClassAtomicRef.value = someClass.freeze()
to
Copy code
someClassAtomicRef.compareAndSet(null, someClass.freeze())
then init will happen only once.
p
ok. got it. the next time, it wont compare to null and hence wont set the variable. thanks
o
another option is to have regular singleton with custom
init
block which fetches config proactively, like it's done in https://github.com/JetBrains/kotlin-native/blob/552984758e4992129d791420a1f266b636b8c26e/samples/tetris/src/tetrisMain/kotlin/Config.kt#L19
p
Thanks. That was helpful.
j
is there no frozen lazy?