0
0
Kotlinprogramming~10 mins

Custom delegated properties in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Custom delegated properties
Define delegate class with getValue and setValue
Create property using 'by' keyword with delegate instance
Access property
Delegate's getValue called
Assign property
Delegate's setValue called
Property value updated
Custom delegated properties use a delegate class to control getting and setting a property via getValue and setValue methods.
Execution Sample
Kotlin
class Delegate {
  operator fun getValue(thisRef: Any?, property: kotlin.reflect.KProperty<*>): String {
    return "Hello"
  }
  operator fun setValue(thisRef: Any?, property: kotlin.reflect.KProperty<*>, value: String) {
    println("Setting value to $value")
  }
}

class Example {
  var prop: String by Delegate()
}

fun main() {
  val e = Example()
  println(e.prop)
  e.prop = "World"
}
This code shows a property 'prop' delegated to a custom Delegate class that prints on set and returns "Hello" on get.
Execution Table
StepActionMethod CalledInput ValueOutput/Effect
1Create Example instanceN/AN/AExample object created with prop delegated
2Access e.propgetValuethisRef=Example instance, property=propReturns "Hello"
3Print e.propN/A"Hello"Outputs: Hello
4Assign e.prop = "World"setValuethisRef=Example instance, property=prop, value="World"Prints: Setting value to World
5End of mainN/AN/AProgram ends
💡 Program ends after property get and set operations complete
Variable Tracker
VariableStartAfter Step 2After Step 4Final
e.propN/A"Hello" (returned by getValue)"World" (set by setValue, no stored value)No stored value, delegate controls behavior
Key Moments - 3 Insights
Why does e.prop print "Hello" even though we never assigned a value before?
Because the delegate's getValue method always returns "Hello" regardless of any assignment, as shown in execution_table step 2.
Does the delegate store the assigned value "World"?
No, the delegate's setValue only prints the assigned value but does not store it, so getValue always returns "Hello" (see steps 4 and 2).
What is the role of 'by Delegate()' in the property declaration?
It tells Kotlin to use the Delegate class to handle getting and setting the property, as shown in concept_flow and execution_table steps.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what does getValue return when e.prop is accessed?
A"World"
Bnull
C"Hello"
DThrows error
💡 Hint
Check execution_table row 2 where getValue returns "Hello"
At which step does the delegate print the assigned value?
AStep 2
BStep 4
CStep 1
DStep 3
💡 Hint
Look at execution_table row 4 where setValue prints the assigned value
If the delegate stored the assigned value, what would change in variable_tracker after step 4?
Ae.prop would show "World" after step 4
Be.prop would still show "Hello" after step 4
Ce.prop would be null after step 4
De.prop would cause an error after step 4
💡 Hint
variable_tracker shows e.prop does not store value now; storing would update it after step 4
Concept Snapshot
Custom delegated properties use a delegate class with operator functions getValue and setValue.
Declare property with 'by Delegate()' to delegate its behavior.
getValue controls what happens when property is read.
setValue controls what happens when property is assigned.
Delegate can store or compute values as needed.
This allows flexible property behavior beyond simple storage.
Full Transcript
Custom delegated properties in Kotlin let you control how a property is read and written by using a delegate class. This class must have operator functions getValue and setValue. When you declare a property with 'by Delegate()', Kotlin calls these functions instead of directly accessing the property. In the example, getValue always returns "Hello" and setValue prints the assigned value but does not store it. The execution table shows step-by-step how the property is accessed and assigned, and the variable tracker shows that the property value is controlled by the delegate, not stored normally. Key moments clarify why the property prints "Hello" even after assignment and the role of the delegate. The visual quiz tests understanding of these steps. This technique allows you to customize property behavior easily.