Skip to content

Instantly share code, notes, and snippets.

@milessabin
Created September 24, 2014 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milessabin/aeecb68caba18f3bb3c0 to your computer and use it in GitHub Desktop.
Save milessabin/aeecb68caba18f3bb3c0 to your computer and use it in GitHub Desktop.
Record and discriminated union type literals and concise value construction in shapeless-2.1.0-SNAPSHOT: https://github.com/milessabin/shapeless/commit/c864b95bb4bb6a1e3ae2569e8820e82586b958f5
scala> import shapeless._, record._, test._, union._
import shapeless._
import record._
import test._
import union._
scala> type Rec = Record.`'i -> Int, 's -> String, 'b -> Boolean`.T // record type literal
defined type alias Rec
scala> val rec: Rec = Record(i = 23, s = "foo", b = true) // concise record value construction
rec: Rec = 23 :: foo :: true :: HNil
scala> rec('i)
res0: Int = 23
scala> rec('s)
res1: String = foo
scala> rec('b)
res2: Boolean = true
scala> type Un = Union.`'i -> Int, 's -> String, 'b -> Boolean`.T // union type literal
defined type alias Un
scala> val un: Un = Union[Un](s = "foo") // concise union value construction
un: Un = foo
scala> un('i)
res3: Option[Int] = None
scala> un('s)
res4: Option[String] = Some(foo)
scala> un('b)
res5: Option[Boolean] = None
@pedrofurla
Copy link

Can you show some fails? Also, the difference between Rec and Un is not clear.

@milessabin
Copy link
Author

Fails are type errors. The difference between the two types is the prefix: Record vs. Union.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment