Scope Functions in Kotlin
From the Kotlin cheat sheet · Extensions · verified Jul 2026
Scope Functions
let, run, with, apply, also
kotlin
// let - it as argument, returns result
val result = "Hello".let {
println("Original: $it")
it.uppercase()
} // HELLO
// Null safety with let
val nullableString: String? = "Kotlin"
nullableString?.let {
println("Length: ${it.length}")
}
// run - this as receiver, returns result
val formatted = "hello".run {
println("Original: $this")
uppercase()
} // HELLO
// with - non-extension, returns result
val numbers = mutableListOf(1, 2, 3)
val sum = with(numbers) {
add(4)
add(5)
sum()
}
// apply - this as receiver, returns receiver
val person = Person().apply {
name = "Alice"
age = 30
email = "alice@example.com"
}
// also - it as argument, returns receiver
val list = mutableListOf(1, 2, 3).also {
println("Adding items to list: $it")
it.add(4)
}💡 let/also use it, run/apply/with use this
⚡ let/run/with return result, apply/also return receiver
📌 Use let for null checks and transformations
🟢 Use apply for object configuration, also for side effects