observable
inline fun <T> observable(initialValue: T, crossinline onChange: (property: KProperty<*>, T, T) -> Unit): ReadWriteProperty<Any?, T>
Content copied to clipboard
Returns a property delegate for a read/write property that calls a specified callback function when changed.
Samples
import kotlin.properties.Delegates
import samples.*
import kotlin.test.*
fun main() {
//sampleStart
var observed = false
var max: Int by Delegates.observable(0) { property, oldValue, newValue ->
observed = true
}
assertPrints(max, "0")
assertFalse(observed)
max = 10
assertPrints(max, "10")
assertTrue(observed)
//sampleEnd
}
Parameters
initial Value
the initial value of the property.
on Change
the callback which is called after the change of the property is made. The value of the property has already been changed when this callback is invoked.