BeginnerNull Safety

Smart Cast

Automatically cast after a type check.

kotlin
fun printLength(value: Any) {
    if (value is String) {
        println(value.length)
    }
}

fun main() {
    printLength("KotlinGuide")
}
Output
11

Explanation

After the is String check, Kotlin automatically treats value as String inside the if block — no manual cast needed.

Related Tutorials