0

I have the following case class

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

I want to be able to have a method that for a column name it can map that value to other, I want it to be typesafe meaning that the funtion f should accept the same type as the symbol 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 = ??? 

How can I make that KV is of the type of the symbol K?

For example

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
Mikel San Vicente
  • 3,831
  • 2
  • 21
  • 39

1 Answers1

2

I've found the way

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 = ???
Mikel San Vicente
  • 3,831
  • 2
  • 21
  • 39