0
0
Kotlinprogramming~10 mins

Extensions for DSL building in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Extensions for DSL building
Define Extension Function
Call Extension in DSL
Build DSL Structure
Use Lambda with Receiver
Configure Object Inside Lambda
Return Configured Object
This flow shows how Kotlin extension functions help build DSLs by adding functions to existing types, using lambdas with receivers to configure objects inside a clean syntax.
Execution Sample
Kotlin
class Robot(var name: String) {
    var speed = 0
}

fun Robot.move(block: Robot.() -> Unit) {
    this.block()
}

fun main() {
    val r = Robot("R2D2")
    r.move { speed = 10 }
    println(r.speed)
}
This code defines an extension function 'move' for Robot that takes a lambda to configure the robot's speed, then prints the speed.
Execution Table
StepActionEvaluationResult
1Create Robot instance r with name 'R2D2'Robot(name='R2D2', speed=0)r = Robot(name='R2D2', speed=0)
2Call extension function r.move with lambda { speed = 10 }Invoke move extensionInside move: this = r
3Execute lambda: speed = 10Set r.speed to 10r.speed = 10
4Exit move functionReturn Unitr.speed remains 10
5Print r.speedOutput value10
💡 Program ends after printing the robot's speed, which is 10
Variable Tracker
VariableStartAfter Step 1After Step 3Final
r.nameundefinedR2D2R2D2R2D2
r.speed001010
Key Moments - 2 Insights
Why can we call 'speed = 10' inside the lambda without specifying 'r.speed'?
Inside the lambda passed to the extension function, 'this' refers to the Robot instance 'r', so we can access its properties directly, as shown in execution_table step 3.
What does the extension function 'move' add to the Robot class?
It adds a new function that takes a lambda with receiver, allowing configuration of Robot instances in a DSL style, demonstrated in execution_table step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of r.speed after step 3?
A10
B0
Cundefined
DError
💡 Hint
Check the 'Result' column in row for step 3 in the execution_table.
At which step is the lambda passed to 'move' executed?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' and 'Evaluation' columns in execution_table rows to find when 'speed = 10' is set.
If we remove 'this.block()' from the 'move' extension, what happens?
ACompilation error
BThe lambda runs automatically, speed becomes 10
CThe lambda is never executed, speed stays 0
DRobot instance is not created
💡 Hint
Refer to execution_table step 2 and 3 where the lambda is explicitly called.
Concept Snapshot
Kotlin extensions let you add functions to existing classes.
Use lambdas with receiver (e.g., Robot.() -> Unit) to build DSLs.
Inside the lambda, 'this' is the receiver object.
Call the lambda inside the extension to apply configuration.
This creates clean, readable DSL-style code.
Full Transcript
This example shows how Kotlin extension functions help build DSLs by adding new functions to existing classes. We define a Robot class with a speed property. Then we add an extension function 'move' that takes a lambda with receiver to configure the Robot instance. When we call r.move { speed = 10 }, the lambda runs with 'this' as the Robot, so we set speed directly. The execution table traces creating the Robot, calling move, running the lambda, and printing the speed. The variable tracker shows how r.speed changes from 0 to 10. Key moments clarify why we can access speed directly inside the lambda and what the extension function adds. The quiz tests understanding of variable values and lambda execution. The snapshot summarizes how extensions and lambdas with receiver enable DSL building in Kotlin.