Any
SC-T1002Although Scala allows methods with return type Any
, it is suggested that you opt for a different and a better approach. This is because dealing with Any
may require calls to isInstanceOf[T]
and asInstanceOf[T]
and if the type argument T
is erased during compilation, any calls to asInstanceOf[T]
and isInstanceOf[T]
will return unreliable results.
List("a", "b", "c").isInstanceOf[List[Int]] // true
1.isInstanceOf[String] // false
The reason statement 1 evaluates to true
is due to Scala's type erasure. So if your method returns entities whose type
can be potentially lost during compilation, any good portion of the code relying on explicit casting
and type checking
becomes unreliable, thus affecting the overall behavior of your program.