| 1 | (* A slice of a const parameter may be returned: it is the caller's |
| 2 | array, not the procedure's frame. Given a temporary, the slice lives |
| 3 | to the end of the caller's statement (report sections 7.3 and 8). *) |
| 4 | module ConstParamSlice; |
| 5 | import Out; |
| 6 | |
| 7 | type Line = record text: [8]U8; len: INT; end; |
| 8 | |
| 9 | var line: Line; |
| 10 | |
| 11 | proc used(const l: Line): []const U8; |
| 12 | begin |
| 13 | return l.text[0..l.len]; |
| 14 | end; |
| 15 | |
| 16 | proc make(s: []const U8): Line; |
| 17 | var l: Line; |
| 18 | begin |
| 19 | COPY(l.text, s); |
| 20 | l.len := LEN(s); |
| 21 | return l; |
| 22 | end; |
| 23 | |
| 24 | proc main*; |
| 25 | var s: []const U8; |
| 26 | begin |
| 27 | line := make("hello"); |
| 28 | s := used(line); |
| 29 | line.text[0] := 'j'; |
| 30 | Out.str(s); Out.ln; (* the caller's array: jello *) |
| 31 | Out.str(used(make("temp"))); Out.ln; (* a temporary, read within the statement *) |
| 32 | Out.int(LEN(used({text: "abc", len: 2}))); Out.ln; |
| 33 | end; |
| 34 | |
| 35 | end ConstParamSlice. |