let with the Elvis operator in Kotlin
Level 4122 keystrokes
?.let runs its block only when name is not null and returns the block's last expression. If name were null, the whole ?.let would be null and the Elvis operator ?: would supply 0 instead.
Kotlin code
// A nullable String holding "Kotlin"1val name: String? = "Kotlin"2 // The block runs only if name is not null; it is the non-null String3val length = name?.let { // Print Name is Kotlin4 println("Name is $it") // The last expression is the block's result: 65 it.length// ?: gives 0 if the left side is null, so length is an Int, never null6} ?: 07 // Print Length: 68println("Length: $length")Output
Name is Kotlin Length: 6
Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this Kotlin code and how often they appear.
"× 6:× 3?× 3.× 2(× 2)× 2=× 2$× 2{× 1}× 1