0
0
Kotlinprogramming~5 mins

Chaining scope functions in Kotlin

Choose your learning style9 modes available
Introduction

Chaining scope functions helps you write clear and short code by running many actions on the same object in a row.

When you want to change or use an object several times without repeating its name.
When you want to set up or configure an object step-by-step in a clean way.
When you want to perform multiple operations on an object and return the final result.
When you want to avoid temporary variables and keep code readable.
When you want to combine actions like modifying, printing, and returning an object smoothly.
Syntax
Kotlin
val result = object.scopeFunction1 {
    // first block using 'this' or 'it'
}.scopeFunction2 {
    // second block
}.scopeFunction3 {
    // third block
}

Scope functions like let, run, also, apply, and with can be chained.

Each function passes the object or the last result to the next, so you can do many things in one expression.

Examples
This example changes text to uppercase, prints it, then reverses it.
Kotlin
val text = "hello"

val result = text
    .let { it.uppercase() }
    .also { println("Uppercase: $it") }
    .let { it.reversed() }

println(result)
This example doubles a number, prints it, then adds 3.
Kotlin
val number = 5

val result = number
    .run { this * 2 }
    .also { println("Doubled: $it") }
    .run { this + 3 }

println(result)
This example adds and removes items from a list, printing after each change.
Kotlin
val list = mutableListOf(1, 2, 3)

list
    .apply { add(4) }
    .also { println("List after add: $it") }
    .apply { removeAt(0) }
    .also { println("List after remove: $it") }
This example checks if a string is empty and prints a message.
Kotlin
val emptyString = ""

val result = emptyString
    .let { if (it.isEmpty()) "Empty" else it }
    .also { println(it) }
Sample Program

This program creates a person, increases their age, prints the updated person, then changes the name to uppercase and prints the final result.

Kotlin
fun main() {
    val person = Person("Alice", 25)

    val updatedPerson = person
        .apply { age += 1 } // Increase age by 1
        .also { println("After birthday: $it") } // Print person
        .run {
            copy(name = name.uppercase()) // Change name to uppercase
        }

    println("Final person: $updatedPerson")
}

data class Person(var name: String, var age: Int)
OutputSuccess
Important Notes

Time complexity depends on the operations inside each scope function, but chaining itself is very fast.

Space complexity is low since no extra large data structures are created during chaining.

A common mistake is confusing which scope function returns the original object and which returns a transformed result.

Use apply or also when you want to return the original object after changes.

Use let or run when you want to return a new value after transformations.

Summary

Chaining scope functions lets you do many things on one object in a clean, readable way.

Different scope functions return either the original object or a new result, so choose based on what you want to get back.

Use chaining to avoid repeating object names and keep your code short and clear.