This code doesn't compile.
class MyClass {
var w: String? = "Hello"
init {
if(w!=null) {
println(w.length)
}
}
}
Compiler error: Smart cast to 'String' is impossible because 'w' is a mutable property that could've been changed by this time. What does this mean? A similar code compiles perfectly.
fun main(args: Array<String>) {
var w: String? = "Hello"
if(w!=null) {
println(w.length)
}
}
They are similar because, as per my understanding, in both cases, the variable w will be instantiated and the if block will run right after it. So why does this code compile perfectly?