I am getting frustrated because I don't know how to handle the frontier between my typesafe code and the external API which uses polymorphism and inheritance.
My flow is the following. I receive an entry value of class Class1, which I use to retrieve from an external service an item of Class Class2. Then I need to subtype on both to obtain the runtime type of them and resolve the implicit. However, this is not possible because of type erasure.
trait Typeclass1[A, B] {
def hash(a: A, b: B): String
}
trait Typeclass2[A, B] {
def hash(a: A, b: B): B
}
trait Entity
trait MyEntity1
trait MyEntity2
object db {
def load(any:Any):Entity = new Entity{}
}
class MyClass[T](t: T, a: String) {
def apply(timeout: Long): T = {
val loadFromDB = db.load(t)
loadFromDB match {
case myEntity1: MyEntity1 => applyTypeSafe(myEntity1)
case myEntity2: MyEntity2 => applyTypeSafe(myEntity2)
}
}
def applyTypeSafe[C](c: C)(implicit typeClass1: Typeclass1[C, T], typeclass2: Typeclass2[C, T]): (String, T) = {
typeClass1.hash(c, t) -> typeclass2.hash(c, t)
}
}
I am wondering what is the right pattern to develop this frontier layer. I would probably need a type-constructor for my typeclass to provide at at the constructor of MyClass... or maybe to totally rethinkg my design?
loadFromDB
and then you have recovered all the type info the DB layer looses. – gzm0 17 hours ago