Kotlin Smart Cast is Awesome!

I don't know Kotlin can do smart cast like this. It is smart enough to cast it for you automatically, so you don't need to explicitly cast anymore.

ยท

2 min read

Kotlin Smart Cast is Awesome!

I totally missed this Kotlin awesome feature in my previous post - Complete C# to Kotlin Syntax Comparisons.

I came to be aware of this smart cast only after 8 months of Kotlin development, and I might have been using it without knowing it. The Android Studio IDE takes care of it!

Let's say you have BaseClass and ChildClass like this.

open class BaseClass
class ChildClass(val value: Int): BaseClass()

and, you create the ChildClass object and being referenced by the BaseClass

val obj: BaseClass = ChildClass(value = 7)

To access the data in ChildClass, you do explicit cast below.

val childObj = obj as ChildClass
childObj.value

But the Kotlin compiler can smart cast automatically for you in the following scenario:

Example 1

fun smartCastExample1() {
    val obj: BaseClass = ChildClass(value = 7)
    //Calling obj.value here is not allowed
    //because obj belongs to BaseClass

    if (obj is ChildClass) {
        //obj in this scope is smart cast to ChildClass.
        //Thus, accessing the obj.value is allowed here
        println("Child value is ${obj.value}")

        //You don't need to explicit cast like this
        val childObj = obj as ChildClass
        println("Child value is ${childObj.value}")
    }
}

obj in this if scope must be ChildClass, thus Kotlin compiler smart casts it

Example 2

fun smartCastExample2() {
    val obj: BaseClass = ChildClass(value = 7)
    if (obj !is ChildClass) return

    // obj is smart cast to ChildClass
    println("Child value is ${obj.value}")
}

obj here must be ChildClass since it has been returned if it is not.

Example 3

fun smartCastExample3() {
    val obj: BaseClass = ChildClass(value = 7)

    // obj at right side of || is smart cast to ChildClass
    if (obj !is ChildClass || obj.value == 7) return

}

This obj.value only gets evaluated when obj is ChildClass. If you change obj !is ChildClass to obj is ChildClass, the smart cast won't work.

Example 4

fun smartCastExample3() {
    val obj: BaseClass = ChildClass(value = 7)

    // obj at right side of && is smart cast to ChildClass
    if (obj is ChildClass && obj.value == 7) return
}

Similar to example above, the second check is only evaluated when obj is ChildClass. If you change obj is ChildClass to obj !is ChildClass, the smart cast won't work.

The example here is cast from super type to subtype. It also works for nullable type to non-nullable type. See more example here.

Did you find this article valuable?

Support Vincent Tsen by becoming a sponsor. Any amount is appreciated!

ย