July 2018
Intermediate to advanced
400 pages
12h 14m
English
Another commonly used standard function is let, which you encountered in Chapter 6.
let scopes a variable to the lambda provided and makes the keyword it, which you learned about in Chapter 5, available to refer to it.
Here is an example of let, which squares the first number in a list:
val firstItemSquared = listOf(1,2,3).first().let {
it * it
}
Without let, you would need to assign the first element to a variable to do the multiplication:
val firstElement = listOf(1,2,3).first()
val firstItemSquared = firstElement * firstElement
When combined with other Kotlin syntax, let provides additional benefits. You saw in Chapter 6 that the null coalescing operator and let can be combined to work on a nullable type. Consider ...
Read now
Unlock full access