DEV Community 👩‍đŸ’ģ👨‍đŸ’ģ

Cover image for Kotlin Smart Cast is Awesome!
Vincent Tsen
Vincent Tsen

Posted on • Updated on • Originally published at vtsen.hashnode.dev

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.

This article was originally published at vtsen.hashnode.dev on May 21, 2022.

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()
Enter fullscreen mode Exit fullscreen mode

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

val obj: BaseClass = ChildClass(value = 7)
Enter fullscreen mode Exit fullscreen mode

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

val childObj = obj as ChildClass
childObj.value
Enter fullscreen mode Exit fullscreen mode

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}")
    }
}

Enter fullscreen mode Exit fullscreen mode

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}")
}
Enter fullscreen mode Exit fullscreen mode

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

}
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

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.


See Also

Top comments (0)

🌚 Browsing with dark mode makes you a better developer by a factor of exactly 40.

It's a scientific fact.