let
The Kotlin standard library function let
can be used for scoping and null-checks. When called on an object, let
executes the given block of code and returns the result of its last expression.
The object is accessible inside the block by the reference it
(by default) or a custom name.
fun customPrint(s: String) {
print(s.uppercase())
}
fun main() {
val empty = "test".let { // 1
customPrint(it) // 2
it.isEmpty() // 3
}
println(" is empty: $empty")
fun printNonNull(str: String?) {
println("Printing \"$str\":")
str?.let { // 4
print("\t")
customPrint(it)
println()
}
}
fun printIfBothNonNull(strOne: String?, strTwo: String?) {
strOne?.let { firstString -> // 5
strTwo?.let { secondString ->
customPrint("$firstString : $secondString")
println()
}
}
}
printNonNull(null)
printNonNull("my string")
printIfBothNonNull("First","Second")
}
- Calls the given block on the result on the string "test".
- Calls the function on "test" by the
it
reference. let
returns the value of this expression.- Uses safe call, so
let
and its code block will be executed only on non-null values. - Uses the custom name instead of
it
, so that the nestedlet
can access the context object of the outerlet
.