我想为从案例类中删除字段构建通用解决方案。使用这个技巧,我构建了这个工作代码:
implicit class SemiGenericIgnoringOps[T](t: T) {
def ignoring[TRepr <: HList,
V,
TargetRepr <: HList,
H <: HList](k: Witness)
(implicit
gen: LabelledGeneric.Aux[T, TRepr],
rem: Remover.Aux[TRepr, k.T, (V, TargetRepr)],
upd: Updater.Aux[TargetRepr, FieldType[k.T, V], H],
ali: Align[H, TRepr]
): SemiGeneric.Aux[T, TargetRepr] = new SemiGeneric[T] {
type Repr = TargetRepr
def convert: TargetRepr = gen.to(t) - k
}
}
我想k: Witness
用HList
of Witness
s 替换 single。但即使添加泛型Witness
类型参数也会导致编译错误:找不到Remover
.
implicit class SemiGenericIgnoringOps[T](t: T) {
def ignoring[TRepr <: HList,
V,
TargetRepr <: HList,
H <: HList,
W <: Witness](w: W) // added type parameter
(implicit
gen: LabelledGeneric.Aux[T, TRepr],
rem: Remover.Aux[TRepr, w.T, (V, TargetRepr)],
upd: Updater.Aux[TargetRepr, FieldType[w.T, V], H],
ali: Align[H, TRepr]
): SemiGeneric.Aux[T, TargetRepr] = new SemiGeneric[T] {
type Repr = TargetRepr
def convert: TargetRepr = gen.to(t) - w
}
}
编译器似乎无法派生Witness.T
。欺骗Witness.Aux[R]
没有帮助。如何克服这个问题?