-
-
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
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
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 |
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
Can you show some fails? Also, the difference between Rec and Un is not clear.