19

Consider the following example:

import shapeless._

case class Foo(bar: String, baz: Boolean)
val labl = LabelledGeneric[Foo]

Now, the type of labl is (prettified)

LabelledGeneric[Foo] {
  type Repr =
    FieldType[Symbol @@ String("bar"), String] ::
    FieldType[Symbol @@ String("baz"), Boolean] ::
    HNil
}

which already conveys the information I need, i.e. the names of the case class fields.

What I'm looking for is a way to go from labl to something along the lines of

"bar" :: "baz" :: HNil

i.e. materializing the information contained in the singleton types into a value.

Is this possible? I could use a macro, but I feel like I would end up rewriting something very similar to the GenericMacros object in shapeless, so I'm wondering whether I can leverage it directly.

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235

1 Answers1

21

You can obtain the keys of the record (as Symbols) via shapeless.ops.record.Keys.

This

import shapeless._
import shapeless.ops.record._

case class Foo(bar: String, baz: Boolean)
val labl = LabelledGeneric[Foo]
val keys = Keys[labl.Repr].apply
println(keys)
println(keys.toList.map(_.name))

results in

'bar :: 'baz :: HNil
List(bar, baz) : List(String)
Utaal
  • 8,484
  • 4
  • 28
  • 37
  • 4
    great, thanks! A minor note: despite what it looks like from the first println, `keys` type is not an `HList` of symbols or strings, but it also contains the tags information. I needed an `HList` of pure strings, so I had to map over the `HList` with a `Poly1` along the lines of `object toName extends Poly1 { implicit def keyToName[A] = at[Symbol with A](_.name) }` – Gabriele Petronella Dec 12 '14 at 16:10