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
- Nullable vs Non-Null Types
- Safe Calls and Elvis
- let, also, and Scope Functions
- Avoiding NullPointerExceptions