In Kotlin, it's common to use let
to execute code if an object (let
receiver) isn't null
, as an alternative to an if != null
check, as in the following:
val nullable: String? = "anything"
nullable?.let {
println(it)
}
In what other situations does it make sense to make use of let
?
FYI, let
is part of Kotlin's stdlib and is defined as follows:
@kotlin.internal.InlineOnly
public inline fun <T, R> T.let(block: (T) -> R): R = block(this)