Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
inline fun <T> observable(
    initialValue: T, 
    crossinline onChange: (property: KProperty<*>, oldValue: T, newValue: T) -> Unit
): ReadWriteProperty<Any?, T>

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

import kotlin.properties.Delegates

import kotlin.test.*

fun main(args: Array<String>) {
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


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.

© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.