August 2017
Intermediate to advanced
440 pages
10h
English
The let function is preferred when we want to use standard functions as if they are extension functions in stream processing:
val newNumber = number.plus(2.0)
.let { pow(it, 2.0) }
.times(2)
Like other extensions, it can be combined with a save call operator:
val newNumber = number?.plus(2.0)
?.let { pow(it, 2.0) }
The let function is also preferred which we just want to unpack a nullable read-write property. In this situation, it is not possible to smart cast this property and we need to shadow it, like in this solution:
var name: String? = null
fun Context.toastName() {
val name = name
if(name != null) {
toast(name)
}
}
The name variable is the shadowing property name, what is necessary if name is a read-write property, ...
Read now
Unlock full access