raz / conform / run / TryOrder.raz

44 lines · 729 B · raw · history · last change b38de572

1(* In t[i()] := try f(), i is called before f, and if f fails nothing
2 is assigned. *)
3module TryOrder;
4import Out;
5
6const Oops: ERROR = "oops";
7
8var t: [3]I64;
9
10proc idx(): INT;
11begin
12 Out.char('i');
13 return 1;
14end;
15
16proc good(): (I64, ERROR);
17begin
18 Out.char('g');
19 return 9, nil;
20end;
21
22proc bad(): (I64, ERROR);
23begin
24 Out.char('b');
25 return 9, Oops;
26end;
27
28proc run(f: proc(): (I64, ERROR)): ERROR;
29begin
30 t[idx()] := try f();
31 return nil;
32end;
33
34proc main*;
35var e: ERROR;
36begin
37 e := run(good);
38 Out.char(' '); Out.int(t[1]); Out.char(' '); Out.str(MSG(e)); Out.ln;
39 t[1] := 0;
40 e := run(bad);
41 Out.char(' '); Out.int(t[1]); Out.char(' '); Out.str(MSG(e)); Out.ln;
42end;
43
44end TryOrder.