aboutsummaryrefslogtreecommitdiff
path: root/tests/run/implicitFuns.scala
diff options
context:
space:
mode:
authorMartin Odersky <odersky@gmail.com>2016-12-05 19:07:02 +0100
committerMartin Odersky <odersky@gmail.com>2016-12-17 18:34:27 +0100
commit4c55d2fc491f7bab2a23a0dc5a53d3c57ad8d2d4 (patch)
tree03921ceeb0422cefe2a8b8bddfec29255507994f /tests/run/implicitFuns.scala
parent43d69ccf3e496c7c02c5dcc40efd2a5e83f6f79c (diff)
downloaddotty-4c55d2fc491f7bab2a23a0dc5a53d3c57ad8d2d4.tar.gz
dotty-4c55d2fc491f7bab2a23a0dc5a53d3c57ad8d2d4.tar.bz2
dotty-4c55d2fc491f7bab2a23a0dc5a53d3c57ad8d2d4.zip
More tests and starting a blog post
Diffstat (limited to 'tests/run/implicitFuns.scala')
-rw-r--r--tests/run/implicitFuns.scala119
1 files changed, 118 insertions, 1 deletions
diff --git a/tests/run/implicitFuns.scala b/tests/run/implicitFuns.scala
index ded1b0c29..ee1d37256 100644
--- a/tests/run/implicitFuns.scala
+++ b/tests/run/implicitFuns.scala
@@ -81,7 +81,7 @@ object Contextual {
def runOn(f: java.io.File): Ctx[Int] = {
val options = List("-verbose", "-explaintypes")
- process(f)(ctx.withBinding(Options, options))
+ process(f).apply(ctx.withBinding(Options, options))
}
def process(f: java.io.File): Ctx[Int] =
@@ -94,3 +94,120 @@ object Contextual {
assert(!compile("a"))
}
}
+
+import collection.mutable.ListBuffer
+
+class Transaction {
+ private val log = new ListBuffer[String]
+ def println(s: String): Unit = log += s
+
+ private var aborted = false
+ private var committed = false
+
+ def abort(): Unit = { aborted = true }
+ def isAborted = aborted
+
+ def commit(): Unit =
+ if (!aborted && !committed) {
+ Console.println("******* log ********")
+ log.foreach(Console.println)
+ committed = true
+ }
+}
+
+object TransactionalExplicit {
+
+ def transaction[T](op: Transaction => T) = {
+ val trans: Transaction = new Transaction
+ op(trans)
+ trans.commit()
+ }
+
+ def f1(x: Int)(implicit thisTransaction: Transaction): Int = {
+ thisTransaction.println(s"first step: $x")
+ f2(x + 1)
+ }
+ def f2(x: Int)(implicit thisTransaction: Transaction): Int = {
+ thisTransaction.println(s"second step: $x")
+ f3(x * x)
+ }
+ def f3(x: Int)(implicit thisTransaction: Transaction): Int = {
+ thisTransaction.println(s"third step: $x")
+ if (x % 2 != 0) thisTransaction.abort()
+ x
+ }
+
+ def main(args: Array[String]) = {
+ transaction {
+ implicit thisTransaction =>
+ val res = f1(args.length)
+ println(if (thisTransaction.isAborted) "aborted" else s"result: $res")
+ }
+ }
+}
+
+object Transactional {
+ type Transactional[T] = implicit Transaction => T
+
+ def transaction[T](op: Transactional[T]) = {
+ implicit val trans: Transaction = new Transaction
+ op
+ trans.commit()
+ }
+
+ def thisTransaction: Transactional[Transaction] = implicitly[Transaction]
+
+ def f1(x: Int): Transactional[Int] = {
+ thisTransaction.println(s"first step: $x")
+ f2(x + 1)
+ }
+ def f2(x: Int): Transactional[Int] = {
+ thisTransaction.println(s"second step: $x")
+ f3(x * x)
+ }
+ def f3(x: Int): Transactional[Int] = {
+ thisTransaction.println(s"third step: $x")
+ if (x % 2 != 0) thisTransaction.abort()
+ x
+ }
+
+ def main(args: Array[String]) = {
+ transaction {
+ val res = f1(args.length)
+ println(if (thisTransaction.isAborted) "aborted" else s"result: $res")
+ }
+ }
+}
+
+object TransactionalExpansion {
+
+ def transaction[T](op: Transaction => T) = {
+ val trans: Transaction = new Transaction
+ op.apply(trans)
+ trans.commit()
+ }
+
+ def thisTransaction = $t: Transaction => $t
+
+ def f1(x: Int) = { $t: Transaction =>
+ thisTransaction.apply($t).println(s"first step: $x")
+ f2(x + 1).apply($t)
+ }
+ def f2(x: Int) = { $t: Transaction =>
+ thisTransaction.apply($t).println(s"second step: $x")
+ f3(x * x).apply($t)
+ }
+ def f3(x: Int) = { $t: Transaction =>
+ thisTransaction.apply($t).println(s"third step: $x")
+ if (x % 2 != 0) thisTransaction.apply($t).abort()
+ x
+ }
+
+ def main(args: Array[String]) = {
+ transaction { $t =>
+ val res = f1(args.length).apply($t)
+ println(if (thisTransaction.apply($t).isAborted) "aborted" else s"result: $res")
+ }
+ }
+}
+