jishindev
07/23/2019, 12:22 PMlazy
in functionality but resettable?thana
07/23/2019, 12:25 PMReadWriteProperty
?Stephan Schroeder
07/23/2019, 12:27 PMstreetsofboston
07/23/2019, 12:27 PMIcaro Temponi
07/23/2019, 4:32 PMfun <T> mutableLazy(initializer: () -> T): MutableLazy<T> = MutableLazy(initializer)
class MutableLazy<T>(val init: () -> T) : ReadWriteProperty<Any?, T> {
private var value: T? = null
override fun getValue(thisRef: Any?, property: KProperty<*>): T {
return when (val v = value) {
null -> {
val initResult = init()
value = initResult
initResult
}
else -> v
}
}
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
this.value = value
}
}
//usage
var someLazyNumber: Int by mutableLazy { 10 }
someLazyNumber = 15
masc3d
07/23/2019, 4:58 PMReadWriteProperty
is probably not a good fit except for most basic implementations.louiscad
07/27/2019, 8:46 AM