Bird
Raised Fist0
Kotlinprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does chaining scope functions in Kotlin help you achieve?
easy
A. Perform multiple operations on one object without repeating its name
B. Create multiple objects at once
C. Write loops faster
D. Automatically convert types

Solution

  1. Step 1: Understand the purpose of scope functions

    Scope functions let you run code blocks on an object, often returning the object or a result.
  2. Step 2: Recognize chaining benefit

    Chaining lets you call many scope functions one after another on the same object without repeating its name.
  3. Final Answer:

    Perform multiple operations on one object without repeating its name -> Option A
  4. Quick Check:

    Chaining scope functions = clean, repeated operations [OK]
Hint: Chaining avoids repeating object names in multiple calls [OK]
Common Mistakes:
  • Thinking chaining creates new objects
  • Confusing chaining with loops
  • Believing chaining changes data types automatically
2. Which of the following is the correct syntax to chain apply and also on an object obj?
easy
A. obj.apply { }.also { }
B. obj.apply() also()
C. apply(obj) { } also { }
D. obj.apply { } also { }

Solution

  1. Step 1: Recall correct chaining syntax

    In Kotlin, scope functions are called with dot notation and lambda blocks inside curly braces.
  2. Step 2: Check each option

    obj.apply { }.also { } uses correct dot chaining with braces. obj.apply() also() misses dots and braces. apply(obj) { } also { } uses wrong function call style. obj.apply { } also { } misses dot before also.
  3. Final Answer:

    obj.apply { }.also { } -> Option A
  4. Quick Check:

    Dot + braces = correct chaining syntax [OK]
Hint: Use dot and braces for each scope function call [OK]
Common Mistakes:
  • Omitting dots between functions
  • Using parentheses instead of braces for lambdas
  • Calling scope functions like regular functions without object
3. What is the output of this Kotlin code?
val result = "hello".also { println(it.uppercase()) }.length
println(result)
medium
A. hello\n5
B. 5\nHELLO
C. HELLO\nhello
D. HELLO\n5

Solution

  1. Step 1: Understand also behavior

    also runs the lambda with the object as it, prints uppercase "HELLO", then returns the original string "hello".
  2. Step 2: Calculate result

    After also, length is called on "hello", which is 5. Then println(result) prints 5.
  3. Final Answer:

    HELLO 5 -> Option D
  4. Quick Check:

    also prints uppercase, length returns 5 [OK]
Hint: also returns original object, so length is on original string [OK]
Common Mistakes:
  • Thinking also returns uppercase string
  • Confusing print order
  • Expecting length to be printed before uppercase
4. Find the error in this Kotlin code chaining scope functions:
val text = "kotlin"
text.let { it.uppercase() }.apply { println(this) }
medium
A. apply cannot be called after let because let returns a different type
B. Missing lambda braces in apply
C. let does not return the original object, so apply prints wrong value
D. No error, code works fine

Solution

  1. Step 1: Analyze let return value

    let returns the lambda result, here it.uppercase(), a new String "KOTLIN".
  2. Step 2: Check apply receiver

    apply is called on the uppercase string, so this inside apply is "KOTLIN" and println(this) prints "KOTLIN".
  3. Step 3: Identify the problem

    The original text is "kotlin", but apply prints the uppercase string, not the original. This may be unexpected if you want to print original.
  4. Final Answer:

    let does not return the original object, so apply prints wrong value -> Option C
  5. Quick Check:

    let returns lambda result, not original object [OK]
Hint: Remember let returns lambda result, not original object [OK]
Common Mistakes:
  • Assuming let returns original object
  • Thinking apply needs braces
  • Believing no error exists
5. Given a mutable list val nums = mutableListOf(1, 2, 3), which chained scope function call correctly adds 4, prints the list, and returns the updated list?
hard
A. nums.let { it.add(4) }.also { println(it) }
B. nums.apply { add(4) }.also { println(it) }
C. nums.also { add(4) }.apply { println(this) }
D. nums.run { add(4); println(this) }

Solution

  1. Step 1: Understand return values of scope functions

    apply returns the original object after running the block, also returns the original object too, let returns lambda result, run returns lambda result.
  2. Step 2: Check each option

    nums.apply { add(4) }.also { println(it) } adds 4 inside apply, then prints list in also, returning the updated list. nums.let { it.add(4) }.also { println(it) }'s let returns Unit from add, so also prints Unit. nums.also { add(4) }.apply { println(this) } has syntax error ('add' unresolved in also block, needs it.add(4)). nums.run { add(4); println(this) } returns Unit from run.
  3. Final Answer:

    nums.apply { add(4) }.also { println(it) } -> Option B
  4. Quick Check:

    apply returns object, also prints it [OK]
Hint: Use apply to modify and also to print, both return original object [OK]
Common Mistakes:
  • Using let which returns Unit from add
  • Expecting run to return the list
  • Confusing order of operations in chaining
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard