How to know if an object is an instance of a TypeTag’s type?

If it suffices to use subtyping checks on erased types, do as Travis Brown suggested in the comment above:

def myIsInstanceOf[T: ClassTag](that: Any) =
  classTag[T].runtimeClass.isInstance(that)

Otherwise you need to explicitly spell out the U type, so that scalac captures its type in a type tag:

def myIsInstanceOf[T: TypeTag, U: TypeTag] =
  typeOf[U] <:< typeOf[T]

Leave a Comment