aboutsummaryrefslogtreecommitdiff
path: root/tests/pending/run/reify_lazyevaluation.scala
diff options
context:
space:
mode:
authorDmitry Petrashko <dmitry.petrashko@gmail.com>2015-05-12 18:30:53 +0200
committerDmitry Petrashko <dmitry.petrashko@gmail.com>2015-05-12 18:30:53 +0200
commit89bacb9c25a58454ff1878e67f7ea07ffc8c269f (patch)
tree51f1ff6c66aebe1b6109b1cffcc2bb8e4cf760a3 /tests/pending/run/reify_lazyevaluation.scala
parenta0fa33deafbea1bf53edc068c5ed9db5592822f9 (diff)
downloaddotty-89bacb9c25a58454ff1878e67f7ea07ffc8c269f.tar.gz
dotty-89bacb9c25a58454ff1878e67f7ea07ffc8c269f.tar.bz2
dotty-89bacb9c25a58454ff1878e67f7ea07ffc8c269f.zip
Run tests as they were in scala.
Diffstat (limited to 'tests/pending/run/reify_lazyevaluation.scala')
-rw-r--r--tests/pending/run/reify_lazyevaluation.scala61
1 files changed, 61 insertions, 0 deletions
diff --git a/tests/pending/run/reify_lazyevaluation.scala b/tests/pending/run/reify_lazyevaluation.scala
new file mode 100644
index 000000000..3f2530dde
--- /dev/null
+++ b/tests/pending/run/reify_lazyevaluation.scala
@@ -0,0 +1,61 @@
+
+import scala.language.{ implicitConversions }
+import scala.reflect.runtime.universe._
+import scala.tools.reflect.Eval
+
+object Test extends App {
+ reify {
+ object lazyLib {
+
+ /** Delay the evaluation of an expression until it is needed. */
+ def delay[A](value: => A): Susp[A] = new SuspImpl[A](value)
+
+ /** Get the value of a delayed expression. */
+ implicit def force[A](s: Susp[A]): A = s()
+
+ /**
+ * Data type of suspended computations. (The name froms from ML.)
+ */
+ abstract class Susp[+A] extends Function0[A]
+
+ /**
+ * Implementation of suspended computations, separated from the
+ * abstract class so that the type parameter can be invariant.
+ */
+ class SuspImpl[A](lazyValue: => A) extends Susp[A] {
+ private var maybeValue: Option[A] = None
+
+ override def apply() = maybeValue match {
+ case None =>
+ val value = lazyValue
+ maybeValue = Some(value)
+ value
+ case Some(value) =>
+ value
+ }
+
+ override def toString() = maybeValue match {
+ case None => "Susp(?)"
+ case Some(value) => "Susp(" + value + ")"
+ }
+ }
+ }
+
+ import lazyLib._
+
+ val s: Susp[Int] = delay { println("evaluating..."); 3 }
+
+ println("s = " + s) // show that s is unevaluated
+ println("s() = " + s()) // evaluate s
+ println("s = " + s) // show that the value is saved
+ println("2 + s = " + (2 + s)) // implicit call to force()
+
+ val sl = delay { Some(3) }
+ val sl1: Susp[Some[Int]] = sl
+ val sl2: Susp[Option[Int]] = sl1 // the type is covariant
+
+ println("sl2 = " + sl2)
+ println("sl2() = " + sl2())
+ println("sl2 = " + sl2)
+ }.eval
+}