tobogganSe connecter

louise

The Louise language: compiler, runtime, std, tooling

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

41 lignes · 840 octetsBrut · Historique
1import std/io
2
3// A panic unwinds through cleanups too. A cleanup that panics while a
4// `final ctl` operation unwinds replaces that unwinding (ADR 0016).
5
6effect fail
7{
8 final ctl fail :: reason: String -> Never
9}
10
11fn attempt :: action: fn() -> a / <fail | e> -> Result<a, String> / <e>
12{
13 handle
14 {
15 Ok(action ())
16 }
17 with
18 { fail reason -> Error(reason) }
19}
20
21pub fn main :: -> () / <io>
22{
23 defer io.println "main cleaned up"
24 io.println "start"
25 let r: Result<Int, String> =
26 attempt
27 fn
28 {
29 defer io.println "outer cleanup still runs"
30 defer panic "cleanup failed"
31 fail "never caught"
32 }
33 match r
34 { Ok(_) -> io.println "not reached"
35 , Error(e) -> io.println ("not reached either: " <> e)
36 }
37}
38//> start
39//> outer cleanup still runs
40//> main cleaned up
41//! panic: cleanup failed