title: Kotlin: lateinit to val, or, alternatively, a var that can set once
body: <p>Just curious: In Kotlin, I would love to get some val that can be initialized by lazy, but with a parameter. That's because I need something that's created very late in order to initialize it.</p>
  
  <p>Specifically, I wish I had:</p>
  
  <pre><code>private lateinit val controlObj:SomeView
  </code></pre>
  
  <p>or:</p>
  
  <pre><code>private val controlObj:SomeView by lazy { view:View-&gt;view.findViewById(...)}
  </code></pre>
  
  <p>and then:</p>
  
  <pre><code>override fun onCreateView(....) {
      val view = inflate(....)
  
  
      controlObj = view.findViewById(...)
  </code></pre>
  
  <p>or in the 2nd case <code>controlObj.initWith(view)</code> or something like that:</p>
  
  <pre><code>return view
  </code></pre>
  
  <p>I cannot use <code>by lazy</code> because <code>by lazy</code> won't accept external parameters to be used when initialising. In this example - the containing <code>view</code>.</p>
  
  <p>Of course I have <code>lateinit var</code> but it would be nice if I could make sure it becomes read only after setting and I could do it in one line.</p>
  
  <p>Is there a pretty clean way to create a read only variable that initializes only once but only when some other variables are born? Any <code>init once</code> keyword? That after init the compiler knows it's immutable? </p>
  
  <p><em>I am aware of the potential concurrency issues here but if I dare to access it before init, I surely deserve to be thrown</em>.</p>