Methods such as .isInstanceOf
and .asInstanceOf
allow you to check if an entity belongs to a certain type and cast an entity to a specified type respectively. Both these methods take in a type parameter T
. Failing to specify this type parameter may permit compilation but will result in a java.lang.ClassCastException
exception. It is recommended that you specify this type parameter.
val isFoo = e.isInstanceOf // always evalutes to false
val foo = e.asInstanceOf // results in `java.lang.ClassCastException`
val isFoo = e.isInstanceOf[Foo]
val foo = e.asInstanceOf[Foo]