Scala

Scala

Made by DeepSource

Explicit cast operation: isInstanceOf SC-T1001

Type check
Major

The instanceOf method allows you to check if an object belongs to a particular type. However, this method should be used with caution. Passing in an invalid type parameter may cause the method to produce unreliable results, especially if performing a fruitless type test.

// Evaluates to true.
List("a", "b", "c").isInstanceOf[List[Int]]

Although List[String] is not the same as List[Int], isInstanceOf still returns true due to the compiler erasing information about generic types of List such as Int and String in this case. Therefore, it is recommended that you resort to match statements when and where possible and rely on .isInstanceOf only when you're absolutely sure what you're doing.

Note: This rule exists for compatibility reasons for users coming from other 3rd party Scala static analysis tools.

References