0
0
Kotlinprogramming~10 mins

Chaining scope functions in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Chaining scope functions
Start with an object
Apply first scope function
Modify or use object inside lambda
Return object or lambda result
Apply next scope function on result
Repeat as needed
Final result or object
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
StepScope FunctionInput ValueLambda ActionLambda ResultReturned ValueOutput
1also"hello"Print 'hello'Unit"hello"hello
2let"hello"Convert to uppercase"HELLO""HELLO"
3run"HELLO"Add ' WORLD'"HELLO WORLD""HELLO WORLD"
4Print final result"HELLO WORLD"PrintUnitHELLO WORLD
💡 All scope functions applied; final string printed.
Variable Tracker
VariableStartAfter alsoAfter letAfter runFinal
resultundefined"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.