File tree Expand file tree Collapse file tree 2 files changed +11
-3
lines changed Expand file tree Collapse file tree 2 files changed +11
-3
lines changed Original file line number Diff line number Diff line change 1
1
1
2
2
failed
3
3
failed
4
+ 2
Original file line number Diff line number Diff line change @@ -12,17 +12,22 @@ object scalax:
12
12
private class Result [T ]:
13
13
var value : T = scala.compiletime.uninitialized
14
14
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 =
16
19
val res = new Result [R ]
17
20
try
18
21
given CanThrow [E ] = ???
19
22
res.value = body
20
23
catch c.asInstanceOf [Throwable => Unit ]
24
+ finally f
21
25
res.value
22
- }
23
26
24
27
extension [R , E <: Exception ](t : (E => Unit ) => R ) def catch1 (c : E => Unit ) = t(c)
25
28
29
+ extension [R , E <: Exception ](c : ( => Unit ) => R ) def finally1 (f : => Unit ) = c(f)
30
+
26
31
import scalax ._
27
32
28
33
def foo (x : Boolean ): Int throws Fail =
@@ -39,9 +44,11 @@ def baz: Int throws Exception = foo(false)
39
44
case ex : Fail =>
40
45
println(" failed" )
41
46
}
42
- try1 {
47
+ try2 {
43
48
println(baz)
44
49
} catch1 {
45
50
case ex : Fail =>
46
51
println(" failed" )
52
+ } finally1 {
53
+ println(2 )
47
54
}
You can’t perform that action at this time.
0 commit comments