I pretty sure that the answer to the question in title is No. But i wrote a simple code and get a weird error when trying to compile. code:
open class Animal (val stttt:String, val str:String = "hjm") {
open var fff: String = ""
open var image = ""
open val food = ""
open val habitat = ""
var hunger: Int? = 10
}
class Hippo ( var strrr:Int = 7) : Animal("just") {
override var image = "hippo.jpg"
override var food = "grass"
override val habitat = "water"
}
fun main(args: Array<String>) {
val hippo: Hippo? = Hippo()
hippo?.hunger = 5
println(hippo?.hunger) // println(hippo?.hunger as Int?) works!!
}
the last line in code:
println(hippo?.hunger) // println(hippo?.hunger as Int?) works!!
makes the compiler display the following error:
smart cast to Int is impossible, because hippo?.hunger is a mutable property that could have been changed by this time
But, if i remove the following line the code is compiled:
hippo?.hunger = 5
Can someone explain me, what is wrong with this line of code that prevents the code from being compiled successfully?