Skip to content

Instantly share code, notes, and snippets.

@milessabin
Created August 2, 2013 14:25
Show Gist options
  • Save milessabin/6140251 to your computer and use it in GitHub Desktop.
Save milessabin/6140251 to your computer and use it in GitHub Desktop.
HLists and singleton types encode records in Scala
scala> import shapeless._; import SingletonTypes._; import Record._
import shapeless._
import SingletonTypes._
import Record._
scala> val r = ("foo" ->> 23) :: ("bar" ->> true) :: ("baz" ->> 2.0) :: HNil
r: (String("foo"), Int) :: (String("bar"), Boolean) :: (String("baz"), Double) :: HNil =
(foo,23) :: (bar,true) :: (baz,2.0) :: HNil
scala> r.head // r is an HList of pairs of singleton-typed Strings and values ...
res0: (String("foo"), Int) = (foo,23)
scala> r.get("foo") // ... it behaves like a record
res1: Int = 23
scala> r.get("bar")
res2: Boolean = true
scala> r.get("baz")
res3: Double = 2.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment