Kotlin: lateinit to val, or, alternatively, a var that can set once

You can implement own delegate like this: class InitOnceProperty<T> : ReadWriteProperty<Any, T> { private object EMPTY private var value: Any? = EMPTY override fun getValue(thisRef: Any, property: KProperty<*>): T { if (value == EMPTY) { throw IllegalStateException(“Value isn’t initialized”) } else { return value as T } } override fun setValue(thisRef: Any, property: KProperty<*>, value: … Read more

isInitialized – Backing field of lateinit var is not accessible at this point

A really simple workaround to the constraints described by the accepted answer is the following: class LateClass { lateinit var thing: Thing fun isThingInitialized() = ::thing.isInitialized } class Client { val lateClass = LateClass() … things happen … if (lateClass.isThingInitialized() { // do stuff with lateClass.thing, safely } }

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)