Created
June 21, 2013 13:03
-
-
Save milessabin/5831007 to your computer and use it in GitHub Desktop.
Case class members with shapeless 2.0.0-SNAPSHOT.
This file contains 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
[info] Starting scala interpreter... | |
[info] | |
Welcome to Scala version 2.10.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_21). | |
Type in expressions to have them evaluated. | |
Type :help for more information. | |
scala> import shapeless._ | |
import shapeless._ | |
scala> class Members[C](c: C) { def members(implicit gen: Generic[C]) = gen.to(c) } | |
defined class Members | |
scala> implicit def toMembers[C](c: C) = new Members(c) | |
toMembers: [C](c: C)Members[C] | |
scala> case class User(first: String, last: String, age: Int) | |
defined class User | |
scala> val foo = User("Foo", "McBar", 25) | |
foo: User = User(Foo,McBar,25) | |
scala> foo.members == "Foo" :: "McBar" :: 25 :: HNil | |
res0: Boolean = true | |
scala> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment