0
0
Kotlinprogramming~10 mins

Apply function behavior and use cases in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Apply function behavior and use cases
Start with an object
Call apply on object
Inside apply: execute block with 'this' as object
Modify object properties
Return the original object
Use modified object
The apply function runs a block on an object, lets you modify it using 'this', and returns the same object for chaining.
Execution Sample
Kotlin
val person = Person().apply {
    name = "Alice"
    age = 30
}
println(person)
Creates a Person, sets name and age inside apply, then prints the modified person.
Execution Table
StepActionObject State (name, age)Return ValueOutput
1Create Person objectname=null, age=0Person instance
2Enter apply blockname=null, age=0Person instance
3Set name = "Alice"name=Alice, age=0Person instance
4Set age = 30name=Alice, age=30Person instance
5Exit apply blockname=Alice, age=30Person instance
6Print personname=Alice, age=30Person instancePerson(name=Alice, age=30)
💡 apply returns the original object after executing the block, allowing property changes.
Variable Tracker
VariableStartAfter Step 3After Step 4Final
person.namenullAliceAliceAlice
person.age003030
Key Moments - 2 Insights
Why does apply return the original object and not the block's last expression?
Because apply is designed to return the object it was called on, enabling chaining. See execution_table step 5 where return value is the Person instance, not the last assignment.
How does 'this' work inside the apply block?
Inside apply, 'this' refers to the object, so you can set properties directly without naming the object. See execution_table steps 3 and 4 where name and age are set without prefix.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the value of person.age?
A0
B30
Cnull
DUnchanged
💡 Hint
Check the Object State column at step 4 in execution_table.
At which step does apply return the original object?
AStep 3
BStep 6
CStep 5
DStep 2
💡 Hint
Look at the Return Value column in execution_table.
If you replaced apply with let, what would change in the return value?
AReturn value would be the last expression in the block
BReturn value would be the original object
CReturn value would be null
DReturn value would be Unit
💡 Hint
Recall that apply returns the object, but let returns the block result.
Concept Snapshot
apply function syntax:
object.apply { this.property = value; ... }
- Executes block with 'this' as object
- Returns the original object
- Useful for initializing or modifying objects inline
- Enables chaining after modifications
Full Transcript
The apply function in Kotlin lets you run a block of code on an object, using 'this' to refer to it. Inside the block, you can set or change properties easily. After the block finishes, apply returns the same object, not the block's last expression. This helps you write clean code to create and modify objects in one step. For example, creating a Person and setting name and age inside apply, then printing the person shows the updated values. Remember, apply returns the original object, so you can chain calls. This is different from let, which returns the block's result.