properties after they've been initialized? Something in the stdlib maybe?
e
ephemient
04/27/2021, 3:28 PM
can't think of anything in stdlib but building your own is straightforward.
Copy code
class LateInitOnce<T : Any> {
private lateinit var value: T
operator fun getValue(thisRef: Any?, property: KProperty<*>): T = value
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
require(!this::value.isInitialized) { "LateInitOnce property has already been initialized" }
this.value = value
}
}
val once by LateInitOnce<String>()
once // throws UninitializedPropertyAccessException
once = "first" // ok
once // ok
once = "second" // throws IllegalStateException