blue
10/19/2021, 11:51 PMstudent as Student from the below code? Don’t I have to explicitly assign the cast result to a variable to access the score property?
abstract class Person(val name: String)
class Student(val score: Int, name: String) : Person(name)
class Teacher(name: String) : Person(name)
fun someFun(student: Person) {
// student as Student
println(student.score)
}Joffrey
10/19/2021, 11:52 PMstudent variable to a more precise type (Student) after the as cast even without using a separate variable (simply because it knows the cast would have failed otherwise).
It's the same principle as null checks that smart cast to non-null type inside the if.blue
10/19/2021, 11:56 PMstudent as Student . But it is not working
assert(student is Student)Joffrey
10/20/2021, 12:01 AMassert doesn't have a contract, or maybe not one that's precise enough to help the compiler here (EDIT: this is actually because assert uses JVM assertions, which can be disabled). OTOH, the cast with as operator is pretty obvious to the compiler, and it's reliable.Joffrey
10/20/2021, 12:02 AMrequire(student is Student) does have a contract that allows this to compile - and it's multiplatformblue
10/20/2021, 12:10 AMcheck(student is Student) is workingJoffrey
10/20/2021, 12:14 AMcheck and require don't throw the same exception (IllegalStateException vs IllegalArgumentException respectively). You should use require if it's input that needs to be checked, and check if you want to verify that some state matches your expectations at some point (e.g. initialization has happened before usage, things like that).