Null-safe chaining in Kotlin

Kotlin

?. stops at the first null and ?: supplies the fallback, replacing a stack of if statements.

val city = user?.address?.city ?: "unknown"

user?.address?.let { addr ->
    println("${addr.street}, ${addr.city}")
}

More in Systems

Random picks