0
0
Kotlinprogramming~10 mins

Observable property delegation in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Observable property delegation
Declare observable property
Set initial value
Change property value
Trigger observer lambda
Observer reacts to change
Property updated with new value
The observable property starts with an initial value. When you change it, a special observer function runs automatically, reacting to the change.
Execution Sample
Kotlin
import kotlin.properties.Delegates

var name: String by Delegates.observable("John") { prop, old, new ->
    println("Property ${prop.name} changed from $old to $new")
}

name = "Jane"
This code creates an observable property 'name' starting as "John". When 'name' changes to "Jane", it prints the change.
Execution Table
StepActionProperty ValueObserver TriggeredOutput
1Declare 'name' with initial value "John"JohnNo
2Assign 'name' = "Jane"JaneYesProperty name changed from John to Jane
3End of codeJaneNo
💡 No more code to run, program ends.
Variable Tracker
VariableStartAfter Step 2Final
nameJohnJaneJane
Key Moments - 2 Insights
Why does the observer lambda run only when the property value changes?
Because the observable delegate calls the observer lambda every time the property is assigned a value, as shown in step 2 of the execution_table.
What do the parameters 'prop', 'old', and 'new' represent in the observer lambda?
'prop' is the property metadata, 'old' is the previous value, and 'new' is the new value assigned. This is shown in the output message in step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'name' after step 1?
A"John"
B"Jane"
Cnull
DUndefined
💡 Hint
Check the 'Property Value' column in row for step 1.
At which step does the observer lambda print the change message?
AStep 3
BStep 1
CStep 2
DNever
💡 Hint
Look at the 'Observer Triggered' and 'Output' columns in the execution_table.
If we assign 'name' = "Jane" again after step 2, what happens?
AObserver lambda triggers and prints change
BObserver lambda does not trigger
CObserver lambda triggers but no print
DProgram crashes
💡 Hint
The observable delegate calls the observer lambda only when the property value actually changes. Assigning the same value does not trigger it.
Concept Snapshot
Observable property delegation in Kotlin:
- Use Delegates.observable(initialValue) { prop, old, new -> ... }
- The lambda runs every time the property is assigned a value
- 'prop' is property info, 'old' is old value, 'new' is new value
- Useful to react automatically when a property changes
Full Transcript
Observable property delegation lets you watch a property for changes. You declare a property with Delegates.observable and give it an initial value. When you assign a new value, the observer lambda runs automatically. It receives the property metadata, the old value, and the new value. This lets you react to changes, like printing a message. This is shown in the example where 'name' changes from "John" to "Jane" and the observer prints the change.