title: Kotlin/Native: Can someone help me in suggesting how to create a frozen singleton that takes in init parameters?
body: <p>Below code will not work because companion object will be created and made immutable as soon as we try to access it like <code>SomeClass.getInstance()</code> or any other property of it and I won't be able to initialise <code>someClass</code> property. I want it to be immutable/frozen (because this will be accessed from multiple threads) but I can't do it when it takes parameters.</p>
  
  <p>Any suggestions?</p>
  
  <pre><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
          }
      }
  }
  </code></pre>
  
  <p>One possible solution is to use AtomicReference here. Like so:</p>
  
  <pre><code>actual open class SomeClass private constructor(private val someProperty: SomeProperty) {
      actual companion object {
          private var someClassAtomicRef: AtomicReference&lt;SomeClass?&gt; = AtomicReference(null)
          fun initialize(someProperty: SomeProperty){
              val someClass = SomeClass(someProperty)
              someClassAtomicRef.value = someClass.freeze()
          }
          actual fun getInstance(): SomeClass {
              return someClassAtomicRef.value ?: throw UninitializedPropertyAccessException("SomeClass is not initialised yet")
          }
      }
  }
  </code></pre>
  
  <p>The problem with above code is that I can call <code>SomeClass.initialise</code> again and have another instance of SomeClass in AtomicReference.</p>
  
  <p>Is there a better way to achieve this?</p>