Chaining scope functions means applying one after another on an object, each modifying or using it, passing the result to the next.
Execution Sample
Kotlin
val result = "hello"
.also { println(it) }
.let { it.uppercase() }
.run { this + " WORLD" }
println(result)
This code chains three scope functions on a string, printing it, converting to uppercase, then adding text.
Execution Table
Step
Scope Function
Input Value
Lambda Action
Lambda Result
Returned Value
Output
1
also
"hello"
Print 'hello'
Unit
"hello"
hello
2
let
"hello"
Convert to uppercase
"HELLO"
"HELLO"
3
run
"HELLO"
Add ' WORLD'
"HELLO WORLD"
"HELLO WORLD"
4
Print final result
"HELLO WORLD"
Print
Unit
HELLO WORLD
💡 All scope functions applied; final string printed.
Variable Tracker
Variable
Start
After also
After let
After run
Final
result
undefined
"hello"
"HELLO"
"HELLO WORLD"
"HELLO WORLD"
Key Moments - 3 Insights
Why does 'also' return the original object instead of the lambda result?
'also' always returns the original object, not the lambda result, as shown in step 1 of the execution_table where returned value is "hello" despite lambda result being Unit.
Why does 'let' return the lambda result instead of the original object?
'let' returns the lambda result, so after step 2, the returned value is "HELLO" which is the uppercase string, not the original "hello".
How does chaining work when the return types differ?
Each scope function returns a value used as input for the next; for example, 'also' returns original object, 'let' returns lambda result, and 'run' returns lambda result, enabling chaining as seen in execution_table steps 1-3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 1, what is the returned value of 'also'?
A"HELLO"
BUnit
C"hello"
D"HELLO WORLD"
💡 Hint
Check the 'Returned Value' column at step 1 in execution_table.
At which step does the string become uppercase?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Lambda Action' and 'Lambda Result' columns in execution_table.
If we replaced 'also' with 'let' in step 1, what would change in the variable_tracker after step 1?
Aresult would be Unit
Bresult would be "hello"
Cresult would be the lambda result of 'let', which is the same as input
Dresult would be null
💡 Hint
Recall that 'let' returns the lambda result, unlike 'also'.
Concept Snapshot
Chaining scope functions:
- Use functions like also, let, run on an object
- Each takes the object as input and returns a value
- also returns original object; let and run return lambda result
- Chain them to transform or use objects step-by-step
- Final result is from last function in chain
Full Transcript
This example shows chaining Kotlin scope functions: also, let, and run. We start with the string "hello". The 'also' function prints it and returns the original string. Then 'let' converts it to uppercase and returns the uppercase string. Next, 'run' adds " WORLD" and returns the new string. Finally, we print the result. Each step passes its return value to the next, enabling chaining. The variable 'result' changes from "hello" to "HELLO" to "HELLO WORLD". This shows how scope functions can be chained to perform multiple operations on an object in a clean way.