Check a value's type and then use it without a manual cast.

Smart Casts

SmartCasts.kt
fun main() {
    val value: Any? = 
    val result: String

    if (value is String) {
        result = "string:${value.length}"
    } else if (value is Int) {
        result = "int:${value + 1}"
    } else {
        result = "missing"
    }

    println("result=$result")
}
fun main() {
    val value: Any? = 
    val result: String

    if (value is String) {
        result = "string:${value.length}"
    } else if (value is Int) {
        result = "int:${value + 1}"
    } else {
        result = "missing"
    }

    println("result=$result")
}
fun main() {
    val value: Any? = 
    val result: String

    if (value is String) {
        result = "string:${value.length}"
    } else if (value is Int) {
        result = "int:${value + 1}"
    } else {
        result = "missing"
    }

    println("result=$result")
}
smart-cast After an `is` check, Kotlin can smart-cast the value inside that branch.