I'm trying to create a generic trait 'Repo' for some types that are subtypes of a trait 'Identifiable'. My plan is to instantiate implementors of 'Repo' by passing a generic TypeTag[HList] that describes the 'Identifiable'-subtypes.
How can I make the compiler guarantee that the types passed in the HList are subtypes of trait 'Identifiable'?
Here's what I've got so far:
//All types in HList must extend Identifiable, how to enforce that at compile time?
trait Repo {
implicit val ltag: TypeTag[L] forSome {type L <: HList}
..
}
trait Identifiable {
..
}
case class Person(..) extends Identifiable
case class Address(..)
//This should compile
class MyRepo
(implicit val ltag: TypeTag[Person :: HNil])
extends Repo {
..
}
//This should not
class MyRepo
(implicit val ltag: TypeTag[Address :: HNil])
extends Repo {
..
}
//HList can contain an unknown number of types
I've seen this question which seems to be related: Type inference on contents of shapeless HList Difference is I don't have an implementation of the HList to work with so not sure how I can calculate the upper bound with types only.