Null Safety Patterns
Safe Calls
Use ?. to read a member only when a value is present.
Safe Calls
SafeCalls.kt
fun main() {
val word: String? =
val hasText = word?.isNotEmpty() == true
val report = if (hasText) "has text" else "no text"
println("word=${word ?: "none"}")
println(report)
}
fun main() {
val word: String? =
val hasText = word?.isNotEmpty() == true
val report = if (hasText) "has text" else "no text"
println("word=${word ?: "none"}")
println(report)
}
fun main() {
val word: String? =
val hasText = word?.isNotEmpty() == true
val report = if (hasText) "has text" else "no text"
println("word=${word ?: "none"}")
println(report)
}
safe-call
The safe-call operator returns `null` instead of calling through a missing value.