diff options
author | Paul Phillips <paulp@improving.org> | 2012-04-04 10:01:47 -0700 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2012-04-04 10:29:29 -0700 |
commit | bb4935e92c26778a1d1096cd5cd66812a9122f66 (patch) | |
tree | 7dcfb28fa551a812e47a62775dde018a62da3857 /test/files | |
parent | a7f68ce32c35c73af855eab26635251249ac171a (diff) | |
download | scala-bb4935e92c26778a1d1096cd5cd66812a9122f66.tar.gz scala-bb4935e92c26778a1d1096cd5cd66812a9122f66.tar.bz2 scala-bb4935e92c26778a1d1096cd5cd66812a9122f66.zip |
Added Option#fold.
Diffstat (limited to 'test/files')
-rw-r--r-- | test/files/run/option-fold.check | 5 | ||||
-rw-r--r-- | test/files/run/option-fold.scala | 19 |
2 files changed, 24 insertions, 0 deletions
diff --git a/test/files/run/option-fold.check b/test/files/run/option-fold.check new file mode 100644 index 0000000000..4e3fe99f98 --- /dev/null +++ b/test/files/run/option-fold.check @@ -0,0 +1,5 @@ +List() +List(5) +-1 +0 +1 diff --git a/test/files/run/option-fold.scala b/test/files/run/option-fold.scala new file mode 100644 index 0000000000..d554ba4f9b --- /dev/null +++ b/test/files/run/option-fold.scala @@ -0,0 +1,19 @@ +object Test { + sealed class A + case object B extends A + case class C(x: Int) extends A + + def f[T](x: Option[T]) = x.fold(List.empty[T])(List(_)) + def g(x: Option[A]) = x.fold(-1) { + case B => 0 + case C(x) => x + } + + def main(args: Array[String]): Unit = { + println(f(None)) + println(f(Some(5))) + println(g(None)) + println(g(Some(B))) + println(g(Some(C(1)))) + } +} |