raz / conform / run / ConstParamSlice.raz

35 lines · 859 B · raw · history · last change 264716cb

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). *)
4module ConstParamSlice;
5import Out;
6
7type Line = record text: [8]U8; len: INT; end;
8
9var line: Line;
10
11proc used(const l: Line): []const U8;
12begin
13 return l.text[0..l.len];
14end;
15
16proc make(s: []const U8): Line;
17var l: Line;
18begin
19 COPY(l.text, s);
20 l.len := LEN(s);
21 return l;
22end;
23
24proc main*;
25var s: []const U8;
26begin
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;
33end;
34
35end ConstParamSlice.