【问题标题】:How to map shapeless HList after zipWithIndex?如何在 zipWithIndex 之后映射无形 HList?
【发布时间】:2019-04-08 04:22:04
【问题描述】:

我想将一些案例类转换为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
  }

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

错误:(24, 35) 找不到参数映射器的隐式值: shapeless.ops.hlist.Mapper[UpdateOps.type,zip.Out] gen.to(tuple).zipWithIndex.map(UpdateOps) 错误:(24, 35) 方法映射的参数不足:(隐式映射器: shapeless.ops.hlist.Mapper[UpdateOps.type,zip.Out])mapper.Out。 未指定的值参数映射器。 gen.to(tuple).zipWithIndex.map(UpdateOps)

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

【问题讨论】:

    标签: scala shapeless


    【解决方案1】:

    您可以使用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)
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-15
      • 2023-03-04
      • 1970-01-01
      • 2017-02-10
      • 1970-01-01
      • 2012-07-11
      • 1970-01-01
      相关资源
      最近更新 更多