简体   繁体   中英

How to map shapeless HList after zipWithIndex?

I want to convert some case class into HList, then zip with index the returned HList and then map it with indexes:

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)
  }
}

Where UpdateOps is a package object:

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

The problem is that i got a compile error:

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)

If i just map HList then there is no errors, but i need to save indexes. Is it possible to achieve it?

You can use the Aux pattern to use the output type of an implicitly resolved type as the input type of the next one:

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)
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM