0
0
Kotlinprogramming~10 mins

Building blocks of type-safe builders in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Building blocks of type-safe builders
Start Builder Function
Create Receiver Object
Apply Lambda with Receiver
Inside Lambda: Configure Object
Return Configured Object
End
The builder function creates an object, applies a lambda to configure it safely, then returns the configured object.
Execution Sample
Kotlin
class Person(var name: String = "", var age: Int = 0)

fun person(block: Person.() -> Unit): Person {
    val p = Person()
    p.block()
    return p
}

val p = person {
    name = "Alice"
    age = 30
}
This code builds a Person object using a type-safe builder lambda to set properties.
Execution Table
StepActionEvaluationResult
1Call person builder functionperson { ... }Enter builder function
2Create Person instanceval p = Person()p = Person(name="", age=0)
3Apply lambda block on pp.block()Inside lambda: set name and age
4Set name propertyname = "Alice"p.name = "Alice"
5Set age propertyage = 30p.age = 30
6Return configured Personreturn pPerson(name="Alice", age=30)
7Assign to variable pval p = ...p = Person(name="Alice", age=30)
💡 Builder function ends after returning the configured Person object.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 5Final
pundefinedPerson(name="", age=0)Person(name="Alice", age=0)Person(name="Alice", age=30)Person(name="Alice", age=30)
Key Moments - 3 Insights
Why can we call 'name = "Alice"' inside the lambda without specifying the object?
Inside the lambda with receiver, 'this' refers to the Person instance 'p', so properties can be accessed directly as shown in step 4.
What ensures that only valid Person properties can be set inside the lambda?
The lambda has a receiver type Person, so only Person's members are accessible, preventing invalid property assignments (see step 3).
Why do we return 'p' at the end of the builder function?
Returning 'p' gives the caller the fully configured Person object after the lambda has applied all changes (step 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'p.name' after step 4?
Anull
B"" (empty string)
C"Alice"
D30
💡 Hint
Check the 'Result' column at step 4 in the execution table.
At which step does the lambda start configuring the Person object?
AStep 3
BStep 2
CStep 5
DStep 7
💡 Hint
Look for 'Apply lambda block on p' in the execution table.
If we remove 'return p' from the builder function, what happens?
AThe function returns the configured Person object anyway.
BThe function returns Unit (no object), so 'p' outside is not assigned.
CThe lambda won't run.
DThe Person object is created twice.
💡 Hint
Refer to step 6 where 'return p' is crucial for returning the object.
Concept Snapshot
Type-safe builders use lambdas with receivers to configure objects.
Syntax: fun builder(block: Receiver.() -> Unit): Receiver
Inside builder: create object, apply block, return object.
Allows direct access to object properties inside lambda.
Ensures only valid properties can be set safely.
Full Transcript
This example shows how a type-safe builder works in Kotlin. The builder function creates a Person object, then applies a lambda with receiver to configure its properties. Inside the lambda, we can set 'name' and 'age' directly because the lambda's receiver is the Person instance. After configuration, the builder returns the fully set up Person object. This pattern helps write clear and safe code for building objects step-by-step.