observable

inline fun <T> observable(initialValue: T, crossinline onChange: (property: KProperty<*>, oldValue: T, newValue: T) -> Unit): ReadWriteProperty<Any?, T>(source)

Returns a property delegate for a read/write property that calls a specified callback function when changed.

Since Kotlin

1.1

Parameters

initialValue

the initial value of the property.

onChange

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.

Samples

import kotlin.properties.Delegates
import kotlin.test.*

fun main() { 
   //sampleStart 
   var observed = false
var max: Int by Delegates.observable(0) { property, oldValue, newValue ->
    observed = true
}

println(max) // 0
println("observed is ${observed}") // false

max = 10
println(max) // 10
println("observed is ${observed}") // true 
   //sampleEnd
}
inline fun <T> observable(initialValue: T, crossinline onChange: (property: KProperty<*>, oldValue: T, newValue: T) -> Unit): ReadWriteProperty<Any?, T>(source)

Returns a property delegate for a read/write property that calls a specified callback function when changed.

Since Kotlin

1.0

Parameters

initialValue

the initial value of the property.

onChange

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.

Samples

import kotlin.properties.Delegates
import kotlin.test.*

fun main() { 
   //sampleStart 
   var observed = false
var max: Int by Delegates.observable(0) { property, oldValue, newValue ->
    observed = true
}

println(max) // 0
println("observed is ${observed}") // false

max = 10
println(max) // 10
println("observed is ${observed}") // true 
   //sampleEnd
}