简体   繁体   English

HList转换出类型

[英]HList transformation Out type

I have created a wrapper on top on HList that can append an HLists . 我在HList顶部创建了一个包装器,可以附加一个HLists It is defined as follows: 定义如下:

class HListWrapper[L <: HList](val hl: L) {
  def append[V, Out <: HList](k: Witness, v: V)(implicit updater: Updater.Aux[L, FieldType[k.T, V], Out],
                                                lk: LacksKey[L, k.T]): HListWrapper[Out] = {
    new HListWrapper(updater(hl, field[k.T](v)))
  }
}
object HListWrapper {
  def apply[P <: Product, L <: HList](p: P)(implicit gen: LabelledGeneric.Aux[P, L]) =
    new HListWrapper(gen.to(p))
}

It used like this: 它像这样使用:

case class CC(i: Int, ii: Int)
val cc = CC(100, 1000)
val hl = HListWrapper(cc)
hl.append('iii, 10000)

But when I try to put the HListWrapper inside another function to capture the type of Out , the compiler cannot seem to resolve the final type of the transformation (fails with type mismatch error): 但是,当我尝试将HListWrapper放入另一个函数中以捕获Out类型时,编译器似乎无法解析转换的最终类型(失败,类型不匹配错误):

def cctohlist[Out <: HList]: CC => HListWrapper[Out] = {
  m => {
    val hl = HListWrapper(m)
    hl.append('iii, 10000)
  }
}

The primary reason to create the cctohlist method is to get the type of the HList after append. 创建cctohlist方法的主要原因是在追加后获取HList的类型。 Is this possible to achieve? 这有可能实现吗?

The following code works: 以下代码有效:

def cctohlist: CC => HListWrapper[Record.`'i -> Int, 'ii -> Int, 'iii -> Int`.T] = {
    m => {
      val hl = HListWrapper(m)
      hl.append('iii, 10000)
    }
  }

When you write 当你写

def cctohlist[Out <: HList]: CC => HListWrapper[Out] = ???

this means I can apply cctohlist[Int :: String :: Boolean :: HNil] and have CC => HListWrapper[Int :: String :: Boolean :: HNil] or I can apply cctohlist[AnyVal :: AnyRef :: Any :: HNil] and have CC => HListWrapper[AnyVal :: AnyRef :: Any :: HNil] etc. This is obviously not the case. 这意味着我可以应用cctohlist[Int :: String :: Boolean :: HNil]并使用CC => HListWrapper[Int :: String :: Boolean :: HNil]或我可以应用cctohlist[AnyVal :: AnyRef :: Any :: HNil]并具有CC => HListWrapper[AnyVal :: AnyRef :: Any :: HNil]等。显然并非如此。


This is generic one: 这是通用的一种:

def cctohlist[P <: Product, L <: HList, Out <: HList](m: P)(implicit
  gen: LabelledGeneric.Aux[P, L],
  updater: Updater.Aux[L, FieldType[Witness.`'iii`.T, Int], Out],
  lk: LacksKey[L, Witness.`'iii`.T]
  ): HListWrapper[Out] = {
    val hl = HListWrapper(m)
    hl.append('iii, 10000)
  }

cctohlist(cc)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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