Skip to content

Instantly share code, notes, and snippets.

@milessabin
Created August 8, 2013 15:18
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milessabin/6185537 to your computer and use it in GitHub Desktop.
Save milessabin/6185537 to your computer and use it in GitHub Desktop.
shapeless records ... current state of play ...
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 SingletonTypes._ ; import Record._
import shapeless._
import SingletonTypes._
import Record._
scala> :paste
// Entering paste mode (ctrl-D to finish)
val book =
("author" ->> "Benjamin Pierce") ::
("title" ->> "Types and Programming Languages") ::
("id" ->> 262162091) ::
("price" ->> 44.11) ::
HNil
// Exiting paste mode, now interpreting.
book: shapeless.::[String with shapeless.Record.KeyType[String("author"),String],shapeless.::[String with shapeless.Record.KeyType[String("title"),String],shapeless.::[Int with shapeless.Record.KeyType[String("id"),Int],shapeless.::[Double with shapeless.Record.KeyType[String("price"),Double],shapeless.HNil]]]] = Benjamin Pierce :: Types and Programming Languages :: 262162091 :: 44.11 :: HNil
scala> book("author") // Note result type ...
res0: String = Benjamin Pierce
scala> book("title") // Note result type ...
res1: String = Types and Programming Languages
scala> book("id") // Note result type ...
res2: Int = 262162091
scala> book("price") // Note result type ...
res3: Double = 44.11
scala> book.keys // Key values are materialized from singleton types encoded in value type
res4: shapeless.::[String("author"),shapeless.::[String("title"),shapeless.::[String("id"),shapeless.::[String("price"),shapeless.HNil]]]] = author :: title :: id :: price :: HNil
scala> book.values
res5: shapeless.::[String,shapeless.::[String,shapeless.::[Int,shapeless.::[Double,shapeless.HNil]]]] = Benjamin Pierce :: Types and Programming Languages :: 262162091 :: 44.11 :: HNil
scala> val newPrice = book("price")+2.0
newPrice: Double = 46.11
scala> val updated = book +("price" ->> newPrice) // Update an existing field
updated: shapeless.::[String with shapeless.Record.KeyType[String("author"),String],shapeless.::[String with shapeless.Record.KeyType[String("title"),String],shapeless.::[Int with shapeless.Record.KeyType[String("id"),Int],shapeless.::[shapeless.Record.FieldType[String("price"),Double],shapeless.HNil]]]] = Benjamin Pierce :: Types and Programming Languages :: 262162091 :: 46.11 :: HNil
scala> updated("price")
res6: Double = 46.11
scala> val extended = updated + ("inPrint" ->> true) // Add a new field
extended: shapeless.::[String with shapeless.Record.KeyType[String("author"),String],shapeless.::[String with shapeless.Record.KeyType[String("title"),String],shapeless.::[Int with shapeless.Record.KeyType[String("id"),Int],shapeless.::[shapeless.Record.FieldType[String("price"),Double],shapeless.::[shapeless.Record.FieldType[String("inPrint"),Boolean],shapeless.HNil]]]]] = Benjamin Pierce :: Types and Programming Languages :: 262162091 :: 46.11 :: true :: HNil
scala> val noId = extended - "id" // Removed a field
noId: shapeless.::[String with shapeless.Record.KeyType[String("author"),String],shapeless.::[String with shapeless.Record.KeyType[String("title"),String],shapeless.::[shapeless.Record.FieldType[String("price"),Double],shapeless.::[shapeless.Record.FieldType[String("inPrint"),Boolean],shapeless.HNil]]]] = Benjamin Pierce :: Types and Programming Languages :: 46.11 :: true :: HNil
scala> noId("id") // Attempting to access a missing field is a compile time error
<console>:25: error: could not find implicit value for parameter selector: shapeless.FieldSelector[shapeless.::[String with shapeless.Record.KeyType[String("author"),String],shapeless.::[String with shapeless.Record.KeyType[String("title"),String],shapeless.::[shapeless.Record.FieldType[String("price"),Double],shapeless.::[shapeless.Record.FieldType[String("inPrint"),Boolean],shapeless.HNil]]]],String("id")]
noId("id")
^
scala>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment