aboutsummaryrefslogtreecommitdiff
path: root/tests/run/generic/SearchResult.scala
diff options
context:
space:
mode:
authorMartin Odersky <odersky@gmail.com>2017-01-31 14:46:24 +1100
committerMartin Odersky <odersky@gmail.com>2017-02-08 19:35:58 +1100
commitfe09e0d8fe68e0b48d5e864e1de12ae5ee86077d (patch)
tree67279b95ae5e7a2f2ebd2418d3a4b12002f107ae /tests/run/generic/SearchResult.scala
parentbc06d17ebfd7c8d6003dffe925c3cf9ebca6b1b9 (diff)
downloaddotty-fe09e0d8fe68e0b48d5e864e1de12ae5ee86077d.tar.gz
dotty-fe09e0d8fe68e0b48d5e864e1de12ae5ee86077d.tar.bz2
dotty-fe09e0d8fe68e0b48d5e864e1de12ae5ee86077d.zip
ADT and Serialization test
The test exercises all the improvements made in previous commits of this branch.
Diffstat (limited to 'tests/run/generic/SearchResult.scala')
-rw-r--r--tests/run/generic/SearchResult.scala63
1 files changed, 63 insertions, 0 deletions
diff --git a/tests/run/generic/SearchResult.scala b/tests/run/generic/SearchResult.scala
new file mode 100644
index 000000000..be8ebd15d
--- /dev/null
+++ b/tests/run/generic/SearchResult.scala
@@ -0,0 +1,63 @@
+package generic
+
+import Shapes._
+
+/** enum SearchResult {
+ * case Success(result: Color)
+ * case Diverging
+ * case NoMatch
+ * case Ambiguous(alt1: SearchResult, alt2: SearchResult)
+ * }
+ */
+sealed trait SearchResult extends Enum
+
+object SearchResult extends EnumValues[SearchResult](2) {
+
+ private def $new(tag: Int, name: String) = new SearchResult {
+ def enumTag = tag
+ override def toString = name
+ registerEnumValue(this)
+ }
+
+ abstract case class Success(result: Color) extends SearchResult {
+ def enumTag = 0
+ }
+ object Success {
+ def apply(result: Color): SearchResult = new Success(result) {}
+ implicit def SuccessShape: Success `shaped` Color =
+ new (Success `shaped` Color) {
+ def toShape(s: Success) = s.result
+ def fromShape(c: Color) = new Success(c) {}
+ }
+ }
+
+ val Diverging = $new(1, "Diverging")
+ val NoMatch = $new(2, "NoMatch")
+
+ abstract case class Ambiguous(alt1: SearchResult, alt2: SearchResult) extends SearchResult {
+ def enumTag = 3
+ }
+ object Ambiguous {
+ def apply(alt1: SearchResult, alt2: SearchResult): SearchResult = new Ambiguous(alt1, alt2) {}
+ implicit def AmbiguousShape: Ambiguous `shaped` Prod[SearchResult, SearchResult] =
+ new (Ambiguous `shaped` Prod[SearchResult, SearchResult]) {
+ def toShape(a: Ambiguous) = Prod(a.alt1, a.alt2)
+ def fromShape(p: Prod[SearchResult, SearchResult]) = new Ambiguous(p.fst, p.snd) {}
+ }
+ }
+
+ implicit def SearchResultShape:
+ SearchResult `shaped` Sum[Success, Sum[Ambiguous, EnumValue[SearchResult]]] =
+ new (SearchResult `shaped` Sum[Success, Sum[Ambiguous, EnumValue[SearchResult]]]) {
+ def toShape(x: SearchResult) = x match {
+ case x: Success => Fst(x)
+ case x: Ambiguous => Snd(Fst(x))
+ case x => Snd(Snd(EnumValue(x.enumTag)))
+ }
+ def fromShape(x: Sum[Success, Sum[Ambiguous, EnumValue[SearchResult]]]): SearchResult = x match {
+ case Fst(s) => s
+ case Snd(Fst(a)) => a
+ case Snd(Snd(ev)) => value(ev.tag)
+ }
+ }
+} \ No newline at end of file