Using Generic#to
, I can get the HList
representation of a case class
:
import shapeless._
case class F(x: Int, y: String)
scala> Generic[F].to( F(1, "foo") )
res1: shapeless.::[Int,shapeless.::[String,shapeless.HNil]] =
1 :: foo :: HNil
However, I'd like to get the following representation:
("x", 1) :: ("y", "foo") :: HNil
In other words, instead of just the F
instance's fields' values, I'd like to get the field names, i.e. x
and y
, as well.
How can I get this representation?
You're looking for
LabelledGeneric
.In order to print the fields, you can then use the
Fields
type class (from theops.record
package), which can be invoked with.fields
if you import also therecord
package.Here's a full example
If you also want to convert the keys to
String
: