【发布时间】:2016-07-04 07:59:29
【问题描述】:
我正在尝试为某些作为特征“可识别”的子类型的类型创建通用特征“回购”。我的计划是通过传递描述“可识别”子类型的通用 TypeTag[HList] 来实例化“回购”的实现者。
如何让编译器保证 HList 中传递的类型是 trait 'Identifiable' 的子类型?
这是我目前得到的:
//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
我看到了这个似乎相关的问题: Type inference on contents of shapeless HList 不同之处在于我没有 HList 的实现可以使用,所以不确定如何仅使用类型计算上限。
【问题讨论】: