Scala 无形压缩问题

Scala shapeless zip issue

在下面的示例中,我得到 无法找到参数 zipper 的隐式值:shapeless.ops.hlist.Zip[shapeless.::[keys.Out,shapeless.::[mapper.Out,shapeless.HNil]]]

我想我需要另一个隐含的,但无法计算出语法。想法?

def mapCCLV[P <: Product, K <: HList, L <: HList, M <: HList](p: P)(poly: Poly1)(
    implicit gen: Generic.Aux[P, L], lab: LabelledGeneric.Aux[P, K], 
    keys: Keys[K], mapper: Mapper.Aux[poly.type, L, M]) = {
  val k = keys.apply
  val v = gen.to(p).map(poly)
  k.zip(v)
}

如错误消息所述,您需要一个额外的隐式参数用于 Zip 类型 class 实例,以便能够压缩。

import shapeless._, shapeless.ops.hlist._, record._, ops.record._, labelled._

def mapCCLV[P <: Product, LG <: HList, K <: HList, G <: HList, M <: HList](
  p: P)(poly: Poly1
)(implicit 
  gen: Generic.Aux[P, G],
  lab: LabelledGeneric.Aux[P, LG], 
  keys: Keys.Aux[LG, K], 
  mapper: Mapper.Aux[poly.type, G, M],
  zip: Zip[K :: M :: HNil]
): zip.Out = {
  val k = keys()
  val v = gen.to(p).map(poly)
  k.zip(v)
}

您更有可能希望使用 ZipWithKeys 而不是 Zip

def mapCCLV[P <: Product, LG <: HList, K <: HList, G <: HList, M <: HList, Out <: HList](
  p: P)(poly: Poly1
)(implicit 
  gen: Generic.Aux[P, G],
  lab: LabelledGeneric.Aux[P, LG], 
  keys: Keys.Aux[LG, K], 
  mapper: Mapper.Aux[poly.type, G, M],
  zwk: ZipWithKeys.Aux[K, M, Out]
) = gen.to(p).map(poly).zipWithKeys(keys())