isInstanceOf
SC-R1029.isInstanceOf[T]
allows you to check if an entity is of type T
or not and is handy when used correctly. There is a simpler and more idiomatic way to write such checks though - using pattern matching. The benefits of pattern matching aside; note that any approach that relies extensively on Any
, .isInstanceOf
and .asInstanceOf
defeats the language's type system and should be avoided.
if (t.isInstanceOf[Int]) {
//
} else if (t.isInstanceOf[Double]) {
//
}
t match {
case _: Int =>
case _: Double =>
}