For these classes:
open class User
class Admin(val name: String) : User()
This compiles:
fun main1() {
val user: User
user = Admin("John")
user.name
}
This does not compile:
fun main2() {
val user: User = Admin("John")
user.name // "Unresolved reference: name"
}
The only difference is whether to split declaration and initialization. Why is the compiler allowed to smartcast user to Admin in the first scenario but not in the second scenario?