Values & References14 / 15
Up next

Smart cast on null check

fun length(s: String?): Int {
    // after this guard, s is known non-null
    if (s == null) return 0
    return s.length
}

fun main() {
    println(length("hello"))
    println(length(null))
}
What you're solving

the compiler narrows the type