I currently have the following (not type safe) api which I'm trying to redesign in a typesafe way:
import cats.instances.list._
import cats.syntax.functorFilter._
sealed trait EnumType
case object A extends EnumType
case object B extends EnumType
case object C extends EnumType
sealed abstract class TypeInfo[T <: EnumType](val enumType: T)
case class Ainfo() extends TypeInfo(A)
case class Ainfo2() extends TypeInfo(A)
case class Binfo() extends TypeInfo(B)
case class Cinfo() extends TypeInfo(C)
//This is the function implemented in a not typesafe way
def filterByEnumType[T <: EnumType: ClassTag](lst: List[TypeInfo[_]]): List[TypeInfo[T]] = {
lst mapFilter { info =>
info.enumType match {
case _: T => Some(info.asInstanceOf[TypeInfo[T]]) //not type safe
case _ => None
}
}
}
filterByEnumType[A.type](List(Ainfo(), Binfo(), Ainfo2(), Cinfo())) //List(Ainfo(), Ainfo2())
Is there an approach to implement it typesafely? Are typemembers useful for such task or probbably shapeless can be used for that?