我正在尝试定义一个类型安全的异构列表,它对其元素的类型有限制,在元素之间强制执行层次结构(例如,类型 A 不能出现在类型 B 之后)。尝试将我的结构转换为无形的 HList 时会出现问题。
以下是我如何为我的类型定义特征:
sealed trait Hierarchy {
type HListType <: HList
def toHList : HListType
def toCaseClass[C](implicit gen: Generic[C]{type Repr >: HListType}) = gen.from(toHList)
}
sealed trait <::[+H <: ElType[_], +T <: Hierarchy] extends Hierarchy {
override type HListType = H :: tail.HListType
val head: H
val tail: T
override def toHList: HListType = head :: tail.toHList
}
我收到以下错误:
Hierarchy.scala:26: covariant type H occurs in invariant position in type shapeless.::[H,<::.this.tail.HListType] of type HListType
这很令人费解,因为定义shapeless.::
两个类型参数都是协变的。
我正在使用 scala 2.11.11 和 shapeless 2.3.2。有没有办法解决这个错误?