如何在 zipWithIndex 之后映射无形 HList?

How to map shapeless HList after zipWithIndex?

我想将一些案例 class 转换成 HList,然后用索引压缩返回的 HList,然后用索引映射它:

class B[A]() {    
  def foo[H <: HList](tuple: A)(implicit gen: Generic.Aux[A, H],
                                               zip: ZipWithIndex[H],
                                               mapper: Mapper[UpdateOps.type, ZipWithIndex[H]#Out]) = {
    gen.to(tuple).zipWithIndex.map(UpdateOps)
  }
}

其中 UpdateOps 是一个包对象:

 object UpdateOps extends Poly1 {
    ??? // not implemented yet
  }

问题是我遇到了一个编译错误:

Error:(24, 35) could not find implicit value for parameter mapper: shapeless.ops.hlist.Mapper[UpdateOps.type,zip.Out] gen.to(tuple).zipWithIndex.map(UpdateOps) Error:(24, 35) not enough arguments for method map: (implicit mapper: shapeless.ops.hlist.Mapper[UpdateOps.type,zip.Out])mapper.Out. Unspecified value parameter mapper. gen.to(tuple).zipWithIndex.map(UpdateOps)

如果我只映射 HList 则没有错误,但我需要保存索引。 有可能实现吗?

您可以使用 Aux 模式将隐式解析类型的输出类型用作下一个类型的输入类型:

class B[A]() {
  def foo[
  H <: HList,
  Z <: HList,
  O <: HList](tuple: A)
             (implicit gen: Generic.Aux[A, H],
              zip: ZipWithIndex.Aux[H, Z],
              mapper: Mapper.Aux[UpdateOps.type, Z, O]): O = {
    gen.to(tuple).zipWithIndex.map(UpdateOps)
  }
}