I have a rule trait that will be mixed-in to another trait called A for applying rule.
trait A[T] {
def map(data: T)
}
trait Rule[E] {
def rule: E => Boolean
}
class AImpl extends A[String] with Rule[String] ... { override def rule ... }
// desired logic
val a = new AImpl() // // get a instance of A
if(a.isInstanceoOf[Rule[String]])
Seq(...).filter(a.asInstanceOf[Rule[String]]rule)
When Rule parameterized with String I know I can directly cast using a.asInstanceOf[Rule[String]]. However I will have other parameterized type later on. But asInstanceOf[Rule[…]] has limited (stated something like https://stackoverflow.com/questions/6686992/scala-asinstanceof-with-parameterized-types)
Is there any recommended way to circumvent this problem? Mainly to achieve the effect that I can dynamically cast Rule with different parameterized type like
if(a.isInstanceOf[Rule[SomeType]]) a.asInstanceOf[Rule[SomeType]] ... // SomeType is the dest type to which I would like to cast.
Thanks