Kotlin Scope Functions Tutorial: let, run, apply, also, and with Explained


This Kotlin Scope Functions tutorial explains the five main scope functions: let, run, apply, also, and with. Each function is explained with practical examples and best practices to help developers write clean, readable, and concise Kotlin code by scoping operations to an object. Scope functions are especially useful for null-safety, object configuration, and functional programming.

Kotlin Scope Functions – Complete Tutorial

Scope Functions Overview

Scope functions allow executing a block of code within the context of an object. They differ in:

  1. Return value – whether they return the object (this) or the lambda result.
  2. Context object – whether this or it is used inside the lambda.

The five main scope functions are: let, run, apply, also, and with.

1. let

  1. Executes code only if the object is not null.
  2. it refers to the object.
  3. Returns the lambda result.

Example


val name: String? = "Muni"

name?.let {
println("Name length: ${it.length}")
}

Best Practices

  1. Use let for null checks and short transformations.
  2. Chain multiple let calls for functional-style operations.

2. run

  1. Executes code in the context of this.
  2. Returns the lambda result.

Example


val result = "Kotlin".run {
println(this.uppercase())
length
}

println("Length: $result")

Best Practices

  1. Use run when you want to compute a value using an object.
  2. Good for initializing objects and returning a computation.

3. apply

  1. Executes code in the context of this.
  2. Returns the object itself.
  3. Commonly used for object initialization.

Example


data class Person(var name: String = "", var age: Int = 0)

val person = Person().apply {
name = "Muni"
age = 35
}

println(person)

Best Practices

  1. Use apply for configuring objects fluently.
  2. Avoid returning values; focus on object setup.

4. also

  1. it refers to the object.
  2. Returns the object itself.
  3. Useful for side-effects like logging.

Example


val numbers = mutableListOf(1, 2, 3)
numbers.also { println("Original list: $it") }
.add(4)
println(numbers) // [1, 2, 3, 4]

Best Practices

  1. Use also for performing operations without affecting the chain of calls.
  2. Ideal for logging or validation steps.

5. with

  1. this refers to the object.
  2. Returns the lambda result.
  3. Not an extension function; requires passing the object as a parameter.

Example


val person = Person("Muni", 35)
val description = with(person) {
"Name: $name, Age: $age"
}
println(description)

Best Practices

  1. Use with when operating on an object multiple times without chaining.
  2. Improves readability by avoiding repeated object references.

Summary

This chapter explained Kotlin’s scope functions: let, run, apply, also, and with. Scope functions help reduce boilerplate code, improve readability, and make operations on objects concise and expressive. Proper use enhances null safety, object configuration, and functional programming patterns.