let, also, and run with Nullable Receivers
Use scope functions to execute code only when a value is non-null.
let, also, and run with Nullable Receivers is a free Kotlin Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.
Scope Functions Meet Nullables
Scope functions let, also, and run pair beautifully with the safe call ?. to execute a block only when the receiver is non-null.
let with Safe Call
obj?.let { ... } runs the block only if obj is non-null; inside, it refers to the non-null value.
fun main() {
val name: String? = "Kotlin"
name?.let {
println("Length: ${it.length}")
println("Upper: ${it.uppercase()}")
}
}let Skips the Block on Null
If the receiver is null, the entire let block is skipped.
fun main() {
val name: String? = null
val result = name?.let {
println("never printed")
it.length
}
println(result) // null
}let Returns a Value
let returns the last expression of its block — chainable into Elvis for defaults.
fun main() {
val raw: String? = " hello "
val cleaned = raw?.let { it.trim().uppercase() } ?: "(empty)"
println(cleaned) // HELLO
}also for Side Effects
also runs a block for its side effects and returns the original receiver — great for logging in a chain.
fun main() {
val name: String? = "Alice"
val len = name?.also { println("Processing: $it") }?.length
println("len = $len")
}also Preserves the Value
Unlike let, also returns the receiver unchanged. Use it when you want to peek without transforming.
fun main() {
val items: List<Int>? = listOf(1, 2, 3)
items?.also { println("Got list of size ${it.size}") }
?.forEach { println(it) }
}run with Safe Call
obj?.run { ... } runs the block with this bound to the receiver. Use when you want member access without the it. prefix.
fun main() {
val name: String? = "Kotlin"
val info = name?.run {
"len=$length upper=${uppercase()}"
}
println(info)
}let vs run
let uses it (lambda argument); run uses this (receiver). Pick whichever reads better.
class User(val name: String, val age: Int)
fun main() {
val user: User? = User("Ada", 35)
user?.let { "${it.name} age ${it.age}" }.also(::println)
user?.run { "$name age $age" }.also(::println)
}Chaining Multiple Scopes
Chain scope functions across a pipeline for stepwise transformation and logging.
fun main() {
val raw: String? = " hello "
val result = raw
?.also { println("raw: '$it'") }
?.let { it.trim() }
?.also { println("trimmed: '$it'") }
?.uppercase()
println(result) // HELLO
}let with takeIf
Combine takeIf (filter) with let for predicate-based execution.
fun main() {
val name: String? = "Kotlin"
name?.takeIf { it.length > 3 }
?.let { println("Long name: $it") }
}Anti-Pattern: Nested ifs
Before scope functions:
fun main() {
val name: String? = "Alice"
// OLD WAY:
if (name != null) {
val upper = name.uppercase()
println(upper)
}
// NEW WAY:
name?.let { println(it.uppercase()) }
}Quick Check
Which scope function returns the receiver unchanged and is mainly used for side effects?
Recap
Combine ?. with let (block returns a value, uses it), run (block returns a value, uses this), or also (returns receiver, for side effects). All skip execution if the receiver is null.
Learn Kotlin with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “let, also, and run with Nullable Receivers” lesson free?
Yes — any 3 lessons of the Kotlin Academy learning path, including “let, also, and run with Nullable Receivers”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.
What will I learn in “let, also, and run with Nullable Receivers”?
Use scope functions to execute code only when a value is non-null. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Academy?
No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
How long does the “let, also, and run with Nullable Receivers” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Academy lesson?
Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Nullable Types and the ? Modifier
- Safe Call ?. and Elvis ?: in Real Code
- let, also, and run with Nullable Receivers
- !! Operator: When and Why to Avoid It