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 Kot…


This content originally appeared on DEV Community and was authored by Vincent Tsen

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()

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.

See Also


This content originally appeared on DEV Community and was authored by Vincent Tsen


Print Share Comment Cite Upload Translate Updates
APA

Vincent Tsen | Sciencx (2022-07-22T21:57:18+00:00) Kotlin Smart Cast is Awesome!. Retrieved from https://www.scien.cx/2022/07/22/kotlin-smart-cast-is-awesome/

MLA
" » Kotlin Smart Cast is Awesome!." Vincent Tsen | Sciencx - Friday July 22, 2022, https://www.scien.cx/2022/07/22/kotlin-smart-cast-is-awesome/
HARVARD
Vincent Tsen | Sciencx Friday July 22, 2022 » Kotlin Smart Cast is Awesome!., viewed ,<https://www.scien.cx/2022/07/22/kotlin-smart-cast-is-awesome/>
VANCOUVER
Vincent Tsen | Sciencx - » Kotlin Smart Cast is Awesome!. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/22/kotlin-smart-cast-is-awesome/
CHICAGO
" » Kotlin Smart Cast is Awesome!." Vincent Tsen | Sciencx - Accessed . https://www.scien.cx/2022/07/22/kotlin-smart-cast-is-awesome/
IEEE
" » Kotlin Smart Cast is Awesome!." Vincent Tsen | Sciencx [Online]. Available: https://www.scien.cx/2022/07/22/kotlin-smart-cast-is-awesome/. [Accessed: ]
rf:citation
» Kotlin Smart Cast is Awesome! | Vincent Tsen | Sciencx | https://www.scien.cx/2022/07/22/kotlin-smart-cast-is-awesome/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.