Skip to content

Commit a292022

Browse files
committed
Add catch method in safe throws strawman
1 parent ab5df22 commit a292022

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

tests/run/safeThrowsStrawman.scala

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,28 @@ def baz: Int throws Exception = foo(false)
2020
class Result[T]:
2121
var value: T = scala.compiletime.uninitialized
2222

23-
def tryCatch[R, E <: Exception](body: => R throws E)(c: E => Unit, f: => Unit = ()): R =
23+
def try1[R, E <: Exception](body: => R throws E): (E => Unit) => R = { c =>
2424
val res = new Result[R]
2525
try
2626
given CanThrow[E] = ???
2727
res.value = body
2828
catch c.asInstanceOf[Throwable => Unit]
29-
finally f
3029
res.value
30+
}
31+
32+
extension [R, E <: Exception](t: (E => Unit) => R) def catch1(c: E => Unit) = t(c)
3133

3234
@main def Test =
33-
tryCatch({
35+
try1({
3436
println(foo(true))
3537
println(foo(false))
36-
})({
38+
}).catch1({
3739
case ex: Fail =>
3840
println("failed")
3941
})
40-
tryCatch(
42+
try1(
4143
println(baz)
42-
)({
44+
).catch1({
4345
case ex: Fail =>
4446
println("failed")
4547
})

0 commit comments

Comments
 (0)