带标签的通用列的提取类型

Extract type of labelled generic column

我有以下情况class

case class Test(field1: Int, field2: String)

我希望能够有一个方法,对于列名,它可以将该值映射到其他值,我希望它是类型安全的,这意味着函数 f 应该接受与符号 K 相同的类型

def mapColumn[A, K, KV, Repr <: HList, Out](obj: A, witness: Witness.Aux[K], f: KV => Out) 
  (implicit mkLens: MkFieldLens[A, K], 
    gen: LabelledGeneric.Aux[A, Repr]): Out = ??? 

如何让KV是符号K的类型?

例如

mapColumn(Test(1, "hello"), 'field1, (v: Int) => v + 1) // this should retrieve 2
mapColumn(Test(1, "hello"), 'field2, (v:String) => v + 1) // this should retrieve "hello1"
mapColumn(Test(1, "hello"), 'field2, (v: Int) => v + 1) // this should be a compilation error

我找到方法了

def mapColumn[A, K, KV, Repr <: HList, Out](obj: A, witness: Witness.Aux[K], f: KV => Out) 
    (implicit mkLens: MkFieldLens[A, K], 
     gen: LabelledGeneric.Aux[A, Repr],
     fieldTypesRest: Selector.Aux[Repr, K, KV]): Out = ???