How to enumerate shapeless Record and access field keys in runtime?

record, scala, shapeless

Solution

You can access the key (which is known at compile-time) as a runtime value through an instance of the `Witness` type class:

object toNamedSingletonListOfValues extends Poly1 {
  implicit def caseField[K, T](implicit wk: Witness.Aux[K]) = 
    at[FieldType[K, T]](field => { wk.value -> List[T](field) })
}

No need for runtime reflection!

Problem

I am writing generic code for processing lists of case class instances, collecting values in each field, combining and then passing it to the library. Using shapeless `LabelledGeneric` and polymorphic functions, it looks like this: ``` object toNamedSingletonListOfValues extends Poly1 { implicit def caseField[K,T] = at[FieldType[K, T]](field => { field.key -> List[T](field) }) } val generic = LabelledGeneric[MyClass] val records = listOfMyClassInstances.map(generic.to) val values = records.map(_.map(toNamedSingletonListOfValues)) // Then combining and passing ``` However, I need a way of getting `field.key` because the library needs the parameter names. Would you mind suggesting the solution?

Original source