0
0
Kotlinprogramming~10 mins

Lambda with receiver concept in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Lambda with receiver concept
Define lambda with receiver
Call lambda on receiver object
Inside lambda: 'this' refers to receiver
Execute lambda body using receiver's members
Return result or Unit
End
The lambda is defined to work with a receiver object, so inside the lambda, 'this' means the receiver. When called, the lambda runs using the receiver's context.
Execution Sample
Kotlin
val result = "Hello".run {
    this.length + 5
}
println(result)
This code runs a lambda on the string "Hello" where 'this' is the string, and returns its length plus 5.
Execution Table
StepActionReceiver ('this')Expression EvaluatedResult
1Call run on "Hello""Hello"Enter lambdaLambda starts with 'this' = "Hello"
2Evaluate this.length"Hello"5Length is 5
3Add 5 to length"Hello"5 + 510
4Return result"Hello"10Lambda returns 10
5Assign to resultN/Aresult = 10result = 10
6Print resultN/Aprintln(10)Output: 10
💡 Lambda finishes after returning 10, which is assigned to result and printed.
Variable Tracker
VariableStartAfter Step 4Final
thisN/A"Hello""Hello"
resultN/AN/A10
Key Moments - 2 Insights
Why does 'this' inside the lambda refer to the string "Hello"?
Because the lambda is called with 'run' on the string, making the string the receiver. See execution_table step 1 where 'this' is set to "Hello".
What happens if we use 'it' instead of 'this' inside the lambda?
'it' is not defined in a lambda with receiver unless explicitly passed. Here, 'this' refers to the receiver object. See execution_table steps 2-3 where 'this.length' is used.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'this.length' at step 2?
A5
B10
CHello
DUndefined
💡 Hint
Check the 'Expression Evaluated' column at step 2 in execution_table.
At which step does the lambda return its final value?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Look for the step where 'Lambda returns' is mentioned in the Result column.
If we changed the lambda to add 10 instead of 5, what would be the final result?
A10
B5
C15
D20
💡 Hint
Add 10 to the length 5 of "Hello" as shown in variable_tracker.
Concept Snapshot
Lambda with receiver syntax:
val result = receiver.run {
  // 'this' refers to receiver
  expression
}

Inside the lambda, 'this' is the receiver object.
Useful to call multiple receiver methods cleanly.
Returns the lambda's last expression value.
Full Transcript
This example shows how a lambda with receiver works in Kotlin. We call the 'run' function on the string "Hello". Inside the lambda, 'this' refers to the string. We get the length of the string using 'this.length', which is 5. Then we add 5 to it, resulting in 10. The lambda returns 10, which is assigned to the variable 'result'. Finally, we print 'result', which outputs 10. The key point is that inside the lambda, 'this' means the receiver object, allowing us to access its members directly.