🐦 Kotlin Intermediate

What is the Delegates.observable in Kotlin?

Answer

Delegates.observable() is a built-in property delegate that calls a handler lambda every time the property value changes, after the change has been applied. var name: String by Delegates.observable("initial") { prop, old, new -> println("Changed from $old to $new") }. The handler receives the property metadata, old value, and new value. It is useful for reacting to property changes for debugging, UI updates, or triggering side effects. For cases where you want to prevent a change, use Delegates.vetoable() instead, which allows the handler to reject the new value.