In Scala, I am grabbing the type of a given class through its Scala manifest and storing it. My question is, how can I then check that type to see if the original class is descended from one parent class or another?
It looks like I can't perform a pattern match on t: Class[MyParentClass]
because of type erasure, as below:
trait Product
trait PerishableProduct extends Product
class Fridge extends Product
class Banana extends PerishableProduct
def getProductType[P <: Product](implicit manifestP: Manifest[P]): Class[P] =
manifestP.erasure.asInstanceOf[Class[P]]
val isPerishable = getProductType[Fridge] match {
case x: Class[PerishableProduct] => true
case _ => false
}
// ^^ warning: non variable type-argument PerishableProduct in type pattern
// Class[PerishableProduct] is unchecked since it is eliminated by erasure
Is there another trick I'm missing?