Skip to content

Commit 727a8bb

Browse files
authored
Merge pull request #11876 from rjolly/try
Add finally method in safe throws strawman 2
2 parents 2cf1152 + 31249ef commit 727a8bb

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

tests/run/safeThrowsStrawman2.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
1
22
failed
33
failed
4+
2

tests/run/safeThrowsStrawman2.scala

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,22 @@ object scalax:
1212
private class Result[T]:
1313
var value: T = scala.compiletime.uninitialized
1414

15-
def try1[R, E <: Exception](body: => R throws E): (E => Unit) => R = { c =>
15+
def try1[R, E <: Exception](body: => R throws E)(c: E => Unit): R =
16+
try2(body)(c) {}
17+
18+
def try2[R, E <: Exception](body: => R throws E)(c: E => Unit)(f: => Unit): R =
1619
val res = new Result[R]
1720
try
1821
given CanThrow[E] = ???
1922
res.value = body
2023
catch c.asInstanceOf[Throwable => Unit]
24+
finally f
2125
res.value
22-
}
2326

2427
extension [R, E <: Exception](t: (E => Unit) => R) def catch1(c: E => Unit) = t(c)
2528

29+
extension [R, E <: Exception](c: ( => Unit) => R) def finally1(f: => Unit) = c(f)
30+
2631
import scalax._
2732

2833
def foo(x: Boolean): Int throws Fail =
@@ -39,9 +44,11 @@ def baz: Int throws Exception = foo(false)
3944
case ex: Fail =>
4045
println("failed")
4146
}
42-
try1 {
47+
try2 {
4348
println(baz)
4449
} catch1 {
4550
case ex: Fail =>
4651
println("failed")
52+
} finally1 {
53+
println(2)
4754
}

0 commit comments

Comments
 (0)