| 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). *) |
| 4 | module EmptySlice; |
| 5 | import Out; |
| 6 | |
| 7 | type Pair = record a, b: INT; end; |
| 8 | |
| 9 | const NotFound: ERROR = "not found"; |
| 10 | |
| 11 | var pairs: [3]Pair; |
| 12 | |
| 13 | (* The pairs from the first whose a is key on, or none and NotFound. *) |
| 14 | proc from(key: INT): ([]Pair, ERROR); |
| 15 | var i: INT; |
| 16 | begin |
| 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; |
| 21 | end; |
| 22 | |
| 23 | proc none: []INT; |
| 24 | begin |
| 25 | return []; |
| 26 | end; |
| 27 | |
| 28 | proc main*; |
| 29 | var s: []Pair; e: ERROR; t, u: []INT; w: [2]INT; |
| 30 | begin |
| 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; |
| 41 | end; |
| 42 | |
| 43 | end EmptySlice. |