0Pricing
Kotlin Academy · Lesson

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 — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “let, also, and run with Nullable Receivers” lesson free?

Yes — the full text of “let, also, and run with Nullable Receivers” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

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

  1. Nullable Types and the ? Modifier
  2. Safe Call ?. and Elvis ?: in Real Code
  3. let, also, and run with Nullable Receivers
  4. !! Operator: When and Why to Avoid It
← Back to Kotlin Academy