Null Safety Patterns
Elvis Operator
Choose a fallback value with ?:.
Elvis Operator
ElvisOperator.kt
fun main() {
val name: String? =
val display = name ?: "anonymous"
val greeting = "hello, $display"
println("display=$display")
println(greeting)
}
fun main() {
val name: String? =
val display = name ?: "anonymous"
val greeting = "hello, $display"
println("display=$display")
println(greeting)
}
fun main() {
val name: String? =
val display = name ?: "anonymous"
val greeting = "hello, $display"
println("display=$display")
println(greeting)
}
elvis
The Elvis operator keeps a present value or returns the expression on the right.