vach
04/12/2017, 10:59 AMclass MutableLazy<T>(val supplier : ()-> T) {
private val mutableOption = MutableOption.empty<T>()
fun getValue() = mutableOption.ifNotPresentCompute(supplier).get()
fun setValue(value : T) = mutableOption.take(value)
fun isInitialized() = mutableOption.isPresent
}
inline operator fun <T> MutableLazy<T>.getValue(thisRef: Any?, property: KProperty<*>): T = getValue()
inline operator fun <T> MutableLazy<T>.setValue(thisRef: Any?, property: KProperty<*>, t: T) {
setValue(t)
}
var something : String by MutableLazy {
log("computing this value")
"default value"
}
fun main(args: Array<String>) {
something = "manually set value" // comment/uncomment this line...
println(something)
}
Consider mutable option just like normal option except not immutable... I can simply replace it with any synchronized wrapper of single generic value...marcinmoskala
04/12/2017, 5:20 PMvach
04/13/2017, 4:30 AMvach
04/13/2017, 4:32 AMvach
04/13/2017, 5:00 AMWe would like to emphasise that extension functions are dispatched statically, i.e. they are not virtual by receiver type
which as i understand mens at runtime it does not do method lookup, it already knows what to invoke, while with polymorph object it will do method lookup...