From 31249ef557d916413fe47e5bea1912ed631c3253 Mon Sep 17 00:00:00 2001 From: Raphael Jolly Date: Wed, 24 Mar 2021 13:06:59 +0100 Subject: [PATCH] Add finally method in safe throws strawman 2 --- tests/run/safeThrowsStrawman2.check | 1 + tests/run/safeThrowsStrawman2.scala | 13 ++++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/tests/run/safeThrowsStrawman2.check b/tests/run/safeThrowsStrawman2.check index 3b8b7ee861f0..1c5b6e6bfd6c 100644 --- a/tests/run/safeThrowsStrawman2.check +++ b/tests/run/safeThrowsStrawman2.check @@ -1,3 +1,4 @@ 1 failed failed +2 diff --git a/tests/run/safeThrowsStrawman2.scala b/tests/run/safeThrowsStrawman2.scala index 490f9c6dbe99..1fa38257c177 100644 --- a/tests/run/safeThrowsStrawman2.scala +++ b/tests/run/safeThrowsStrawman2.scala @@ -12,17 +12,22 @@ object scalax: private class Result[T]: var value: T = scala.compiletime.uninitialized - def try1[R, E <: Exception](body: => R throws E): (E => Unit) => R = { c => + def try1[R, E <: Exception](body: => R throws E)(c: E => Unit): R = + try2(body)(c) {} + + def try2[R, E <: Exception](body: => R throws E)(c: E => Unit)(f: => Unit): R = val res = new Result[R] try given CanThrow[E] = ??? res.value = body catch c.asInstanceOf[Throwable => Unit] + finally f res.value - } extension [R, E <: Exception](t: (E => Unit) => R) def catch1(c: E => Unit) = t(c) + extension [R, E <: Exception](c: ( => Unit) => R) def finally1(f: => Unit) = c(f) + import scalax._ def foo(x: Boolean): Int throws Fail = @@ -39,9 +44,11 @@ def baz: Int throws Exception = foo(false) case ex: Fail => println("failed") } - try1 { + try2 { println(baz) } catch1 { case ex: Fail => println("failed") + } finally1 { + println(2) }