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
.
b
blue
10/19/2021, 11:56 PM
Iโd imagine it will work if I add the below statement instead of the
student as Student
. But it is not working
Copy code
assert(student is Student)
j
Joffrey
10/20/2021, 12:01 AM
I guess
assert
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 AM
Note that
require(student is Student)
does have a contract that allows this to compile - and it's multiplatform
๐ 1
๐ 1
b
blue
10/20/2021, 12:10 AM
Even
check(student is Student)
is working
j
Joffrey
10/20/2021, 12:14 AM
Yes, that works too. But keep in mind that they don't have the same semantics.
check
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).