Skip to content

Instantly share code, notes, and snippets.

@milessabin
Created August 6, 2013 16:26
Show Gist options
  • Save milessabin/6166101 to your computer and use it in GitHub Desktop.
Save milessabin/6166101 to your computer and use it in GitHub Desktop.
Encoding records in #Scala as an HList of values tagged with the singleton types of their keys.
scala> import SingletonTypes._ ; import Record._
import SingletonTypes._
import Record._
scala> val r = "name" ->> "foo" :: "age" ->> 1 :: "lastName" ->> "bar" :: HNil
r: shapeless.::[String with Object{type keyType = String("name")},shapeless.::[Int with Object{type keyType = String("age")},shapeless.::[String with Object{type keyType = String("lastName")},shapeless.HNil]]] = foo :: 1 :: bar :: HNil
scala> r(0)
res0: String with Object{type keyType = String("name")} = foo
scala> r(1)
res1: Int with Object{type keyType = String("age")} = 1
scala> r(2)
res2: String with Object{type keyType = String("lastName")} = bar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment