Kotlin 2 · Basics and null safety

Check a type and use it

After an is check, Kotlin smart-casts the value to that type.

fun describe(x: Any) = when (x) {
    is String -> "text of length ${x.length}"
    is Int -> "number ${x + 1}"
    else -> "something else"
}

Official Kotlin 2 documentation

More basics and null safety commands

All basics and null safety commands · Kotlin 2 cheatsheet

Was this page helpful?