Created
August 2, 2013 14:25
-
-
Save milessabin/6140251 to your computer and use it in GitHub Desktop.
HLists and singleton types encode records in Scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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