raz / conform / run / EmptySlice.raz

43 lines · 1.1 KiB · raw · history · last change fa020166

1(* [] used as a slice is the empty slice, as a slice variable starts,
2 and no variable of the procedure: it may be returned beside an error
3 (report 2026-10.30, sections 6.5 and 7.3). *)
4module EmptySlice;
5import Out;
6
7type Pair = record a, b: INT; end;
8
9const NotFound: ERROR = "not found";
10
11var pairs: [3]Pair;
12
13(* The pairs from the first whose a is key on, or none and NotFound. *)
14proc from(key: INT): ([]Pair, ERROR);
15var i: INT;
16begin
17 for i := 0 to LEN(pairs) - 1 do
18 if pairs[i].a = key then return pairs[i..LEN(pairs)], nil; end;
19 end;
20 return [], NotFound;
21end;
22
23proc none: []INT;
24begin
25 return [];
26end;
27
28proc main*;
29var s: []Pair; e: ERROR; t, u: []INT; w: [2]INT;
30begin
31 pairs := [{a: 1, b: 10}, {a: 2, b: 20}, {a: 3, b: 30}];
32 s, e := from(2);
33 Out.int(LEN(s)); Out.str(" "); Out.int(s[0].b); Out.ln;
34 s, e := from(9);
35 if e = NotFound then Out.str("not found, "); Out.int(LEN(s)); Out.ln; end;
36 t := none();
37 w := t.([2]INT); (* ptr and len (5.3) *)
38 if (w[0] = 0) and (w[1] = 0) then Out.str("empty, as a variable starts\n"); end;
39 u := [];
40 Out.int(LEN(u)); Out.ln;
41end;
42
43end EmptySlice.