Is Kotlin's singleton thread-safe?
# getting-started
t
Is Kotlin's singleton thread-safe?
d
Someone correct me if I'm wrong. On the Native and JS backend it doesn't matter. On the JVM backend, it is the same as Java.
t
Java does not provide singleton on language level. what do you saying ?
also, Kotlin/Native has worker model, but it uses thread in behind. "it doesn't matter" is not correct.
a
According documentation, it is. https://kotlinlang.org/docs/reference/object-declarations.html#object-declarations For JVM (according decompiled version of simple object), it is initialized in static block and static class init is guaranteed to be thread-safe AFAIK
👍 1
d
By "it doesn't matter", I meant you can't have shared mutable state. So you do not need to think about Thread-safety.
k
object MyState { var foo = 4 }