diff options
author | Martin Odersky <odersky@gmail.com> | 2016-12-13 14:58:26 +0100 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2016-12-17 18:34:27 +0100 |
commit | 6eb1a72bce041d4fd8519713031894dac3192025 (patch) | |
tree | 61909b6fcab49b310b9d4f3f82e66fe82f772798 /tests/run/implicitFuns.scala | |
parent | 5e2f7d18d711814cecce62a4bd7315a26a5efb87 (diff) | |
download | dotty-6eb1a72bce041d4fd8519713031894dac3192025.tar.gz dotty-6eb1a72bce041d4fd8519713031894dac3192025.tar.bz2 dotty-6eb1a72bce041d4fd8519713031894dac3192025.zip |
New ShortcutImplicits phase
Optimizes implicit closures by avoiding closure
creation where possible.
Diffstat (limited to 'tests/run/implicitFuns.scala')
-rw-r--r-- | tests/run/implicitFuns.scala | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/tests/run/implicitFuns.scala b/tests/run/implicitFuns.scala index ee1d37256..1bc5a2f6b 100644 --- a/tests/run/implicitFuns.scala +++ b/tests/run/implicitFuns.scala @@ -211,3 +211,47 @@ object TransactionalExpansion { } } +object TransactionalAbstracted { + type Transactional[T] = implicit Transaction => T + + trait TransOps { + def thisTransaction: Transactional[Transaction] + def f1(x: Int): Transactional[Int] + def f2(x: Int): Transactional[Int] + def f3(x: Int): Transactional[Int] + } + + object TransOpsObj extends TransOps { + + 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 + } + } + + val transOps: TransOps = TransOpsObj + + def transaction[T](op: Transactional[T]) = { + implicit val trans: Transaction = new Transaction + op + trans.commit() + } + + def main(args: Array[String]) = { + transaction { + val res = transOps.f1(args.length) + println(if (transOps.thisTransaction.isAborted) "aborted" else s"result: $res") + } + } +} |