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.
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 thisif
scope must beChildClass
, 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 beChildClass
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 whenobj
isChildClass
. If you changeobj !is ChildClass
toobj 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
isChildClass
. If you changeobj is ChildClass
toobj !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.
Originally published at https://vtsen.hashnode.dev.
Top comments (0)