Skip to content

Commit ab5df22

Browse files
committed
Add try method in safe throws strawman
1 parent a081a74 commit ab5df22

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

tests/run/safeThrowsStrawman.scala

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,29 @@ def foo(x: Boolean): Int throws Fail =
1717
def bar(x: Boolean)(using CanThrow[Fail]): Int = foo(x)
1818
def baz: Int throws Exception = foo(false)
1919

20-
@main def Test =
20+
class Result[T]:
21+
var value: T = scala.compiletime.uninitialized
22+
23+
def tryCatch[R, E <: Exception](body: => R throws E)(c: E => Unit, f: => Unit = ()): R =
24+
val res = new Result[R]
2125
try
22-
given CanThrow[Fail] = ???
26+
given CanThrow[E] = ???
27+
res.value = body
28+
catch c.asInstanceOf[Throwable => Unit]
29+
finally f
30+
res.value
31+
32+
@main def Test =
33+
tryCatch({
2334
println(foo(true))
2435
println(foo(false))
25-
catch case ex: Fail =>
26-
println("failed")
27-
try
28-
given CanThrow[Exception] = ???
36+
})({
37+
case ex: Fail =>
38+
println("failed")
39+
})
40+
tryCatch(
2941
println(baz)
30-
catch case ex: Fail =>
31-
println("failed")
42+
)({
43+
case ex: Fail =>
44+
println("failed")
45+
})

0 commit comments

Comments
 (0)