Karlo Lozovina
04/27/2021, 3:19 PMlatinit
properties after they've been initialized? Something in the stdlib maybe?ephemient
04/27/2021, 3:28 PMclass 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
nfrankel
04/27/2021, 3:28 PMnfrankel
04/27/2021, 3:29 PMephemient
04/27/2021, 3:29 PMnfrankel
04/27/2021, 3:35 PMKarlo Lozovina
04/27/2021, 5:31 PM