It has been said that using instanceOf is redundant in Scala. However, how would you test for an object passed into a method being either one of two different types without using it?
final class IsAorB[T] private ()
object IsAorB {
implicit val aIsAorB: IsAorB[A] = new IsAorB[A]()
implicit val bIsAorB: IsAorB[B] = new IsAorB[B]()
}
import IsAorB._
def mine[T](that: T)(implicit evidence: IsAorB[T]): Unit = {
require(evidence ne null) // optional
...
}
Given the situation you’re describing, sure, isInstanceOf might be correct.
That said – you should try hard to avoid getting into this situation. With a few specific exceptions (such as interfacing with JavaScript, or certain situations in Akka), winding up with a raw Any tends to mean something’s gone awry in the code, and the right solution is to examine the codepaths to avoid losing your type information in the first place.