0Pricing
Android Academy · Lesson

let, also, and Scope Functions

Work with nullable values.

What Are Scope Functions?

Scope functions let you run a block of code in the context of an object. They make code more concise, especially around nullable values.

This lesson focuses on let and also, the two most useful for null safety, and how they relate to the safe call operator.

The let Function

let runs a block and passes the object in as it. It returns whatever the block returns.

It is handy for transforming a value or scoping a temporary name.

fun main() {
    val length = "Kotlin".let { it.length }
    println(length)  // prints: 6
}

All lessons in this course

  1. Nullable vs Non-Null Types
  2. Safe Calls and Elvis
  3. let, also, and Scope Functions
  4. Avoiding NullPointerExceptions
← Back to Android Academy