| 1 | <!doctype html> |
| 2 | <html lang="en"> |
| 3 | <head> |
| 4 | <meta charset="utf-8"> |
| 5 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 6 | <title>Records and pointers - Raz, Set, Go</title> |
| 7 | <meta name="description" content="raz: a small systems language in Wirth's tradition, for x86-64 Linux."> |
| 8 | <meta name="theme-color" content="#E07A5F"> |
| 9 | <meta property="og:title" content="Records and pointers - Raz, Set, Go"> |
| 10 | <meta property="og:description" content="A small systems language for x86-64 Linux. No run time, no GC, one report."> |
| 11 | <meta property="og:image" content="https://raz.lucela.org/social.png"> |
| 12 | <meta name="twitter:card" content="summary_large_image"> |
| 13 | <link rel="icon" href="../favicon.svg" type="image/svg+xml"> |
| 14 | <link rel="icon" href="../favicon-32.png" type="image/png" sizes="32x32"> |
| 15 | <link rel="apple-touch-icon" href="../apple-touch-icon.png"> |
| 16 | <link rel="stylesheet" href="../style.css"> |
| 17 | </head> |
| 18 | <body> |
| 19 | <header><nav><a class="mark" href="../index.html"><img src="../logo.svg" width="26" height="26" alt="">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">More Raz</a><a href="../spec.html">Report</a><a href="../source/index.html">Source</a><a href="../index.html#download">Download</a></nav></header> |
| 20 | <main> |
| 21 | <div class="tour"><aside><h2>Raz, Set, Go</h2><h3>Raz</h3><ol start='1'> |
| 22 | <li><a href="hello.html">Hello, world</a></li> |
| 23 | <li><a href="modules.html">Modules</a></li> |
| 24 | <li><a href="integers.html">Integers</a></li> |
| 25 | <li><a href="values.html">Values and conversions</a></li> |
| 26 | <li><a href="procedures.html">Procedures and several results</a></li> |
| 27 | </ol> |
| 28 | <h3>Set</h3><ol start='6'> |
| 29 | <li><a href="slices.html">Arrays, slices and strings</a></li> |
| 30 | <li><a href="records.html" aria-current="page">Records and pointers</a></li> |
| 31 | <li><a href="variants.html">Enumerations, sets, variants and match</a></li> |
| 32 | <li><a href="memory.html">Memory and allocators</a></li> |
| 33 | </ol> |
| 34 | <h3>Go</h3><ol start='10'> |
| 35 | <li><a href="errors.html">Errors</a></li> |
| 36 | <li><a href="defer.html">Defer</a></li> |
| 37 | <li><a href="checks.html">When it goes wrong</a></li> |
| 38 | <li><a href="generics.html">Generic modules</a></li> |
| 39 | <li><a href="c.html">Talking to C</a></li> |
| 40 | <li><a href="threads.html">Threads</a></li> |
| 41 | </ol></aside><article><p class="kicker">Set · lesson 7 of 15</p><h1>Records and pointers</h1> |
| 42 | <div class="file"><span>Boxes.raz</span></div><pre class="code"><code><span class="kw">module</span> Boxes; |
| 43 | <span class="kw">import</span> Fmt, SYSTEM; |
| 44 | |
| 45 | <span class="kw">type</span> |
| 46 | Point = <span class="kw">record</span> x, y: <span class="ty">INT</span>; <span class="kw">end</span>; |
| 47 | Box = <span class="kw">record</span> lo, hi: Point; <span class="kw">end</span>; |
| 48 | |
| 49 | <span class="kw">proc</span> area(b: Box): <span class="ty">INT</span>; |
| 50 | <span class="kw">begin</span> |
| 51 | <span class="kw">return</span> (b.hi.x - b.lo.x) * (b.hi.y - b.lo.y); |
| 52 | <span class="kw">end</span>; |
| 53 | |
| 54 | <span class="kw">proc</span> grow(b: ^Box; n: <span class="ty">INT</span>); |
| 55 | <span class="kw">begin</span> |
| 56 | <span class="ty">DEC</span>(b^.lo.x, n); |
| 57 | <span class="ty">DEC</span>(b^.lo.y, n); |
| 58 | <span class="ty">INC</span>(b^.hi.x, n); |
| 59 | <span class="ty">INC</span>(b^.hi.y, n); |
| 60 | <span class="kw">end</span>; |
| 61 | |
| 62 | <span class="kw">proc</span> main*; |
| 63 | <span class="kw">var</span> b: Box; p: ^Point; |
| 64 | <span class="kw">begin</span> |
| 65 | b := {lo: {x: <span class="number">1</span>, y: <span class="number">1</span>}, hi: {x: <span class="number">4</span>, y: <span class="number">3</span>}}; |
| 66 | Fmt.print(<span class="string">"{}\n"</span>, [Fmt.int(area(b))]); |
| 67 | grow(SYSTEM.<span class="ty">ADR</span>(b), <span class="number">1</span>); |
| 68 | p := SYSTEM.<span class="ty">ADR</span>(b.hi); |
| 69 | p^.x := <span class="number">10</span>; |
| 70 | Fmt.print(<span class="string">"{} {}\n"</span>, [Fmt.int(area(b)), Fmt.int(b.hi.x)]); |
| 71 | <span class="kw">end</span>; |
| 72 | |
| 73 | <span class="kw">end</span> Boxes.</code></pre> |
| 74 | <pre class="output"><code>6 |
| 75 | 40 10 |
| 76 | </code></pre> |
| 77 | <p>A record is laid out as C lays out a struct. <code>{lo: ..., hi: ...}</code> builds one, naming its fields; the ones it leaves out are zero. <code>^Box</code> is a pointer to a <code>Box</code>, <code>SYSTEM.ADR(b)</code> the address of <code>b</code>, and <code>b^</code> what a pointer points at. A pointer is never the record: it is <code>b^.lo</code>, never <code>b.lo</code>, so you always see where the memory is reached through an address.</p> |
| 78 | <h2>Copies and literals</h2> |
| 79 | <p>Assigning a record copies it whole, as with an array. A record is compared field by field by writing the comparison, not by <code>=</code>.</p> |
| 80 | <div class="file"><span>Points.raz</span></div><pre class="code"><code><span class="kw">module</span> Points; |
| 81 | <span class="kw">import</span> Out; |
| 82 | |
| 83 | <span class="kw">type</span> Point = <span class="kw">record</span> x, y: <span class="ty">INT</span>; <span class="kw">end</span>; |
| 84 | |
| 85 | <span class="kw">proc</span> show(p: Point); |
| 86 | <span class="kw">begin</span> |
| 87 | Out.char(<span class="string">'('</span>); Out.int(p.x); Out.str(<span class="string">", "</span>); Out.int(p.y); Out.char(<span class="string">')'</span>); |
| 88 | <span class="kw">end</span>; |
| 89 | |
| 90 | <span class="kw">proc</span> mirror(p: Point): Point; |
| 91 | <span class="kw">begin</span> |
| 92 | <span class="kw">return</span> {x: p.y, y: p.x}; |
| 93 | <span class="kw">end</span>; |
| 94 | |
| 95 | <span class="kw">proc</span> main*; |
| 96 | <span class="kw">var</span> a, b: Point; |
| 97 | <span class="kw">begin</span> |
| 98 | a := {x: <span class="number">3</span>, y: <span class="number">4</span>}; |
| 99 | b := a; <span class="comment">(* a copy *)</span> |
| 100 | b.x := <span class="number">30</span>; |
| 101 | show(a); show(b); show(mirror(a)); Out.ln; |
| 102 | a := {y: <span class="number">9</span>}; <span class="comment">(* x is zero *)</span> |
| 103 | show(a); Out.ln; |
| 104 | <span class="kw">end</span>; |
| 105 | |
| 106 | <span class="kw">end</span> Points.</code></pre> |
| 107 | <pre class="output"><code>(3, 4)(30, 4)(4, 3) |
| 108 | (0, 9) |
| 109 | </code></pre> |
| 110 | <h2>Tables of constants</h2> |
| 111 | <p>A constant may be a record, or an array of them: a table the program reads but can't change. Its fields are integers, reals, <code>BOOL</code>s, strings, procedures and the like, and arrays and records of these.</p> |
| 112 | <div class="file"><span>Planets.raz</span></div><pre class="code"><code><span class="kw">module</span> Planets; |
| 113 | <span class="kw">import</span> Fmt; |
| 114 | |
| 115 | <span class="kw">type</span> Planet = <span class="kw">record</span> name: []<span class="kw">const</span> <span class="ty">U8</span>; moons: <span class="ty">INT</span>; water: <span class="ty">BOOL</span>; <span class="kw">end</span>; |
| 116 | |
| 117 | <span class="kw">const</span> Inner: []Planet = [ |
| 118 | {name: <span class="string">"Mercury"</span>}, |
| 119 | {name: <span class="string">"Venus"</span>}, |
| 120 | {name: <span class="string">"Earth"</span>, moons: <span class="number">1</span>, water: <span class="ty">true</span>}, |
| 121 | {name: <span class="string">"Mars"</span>, moons: <span class="number">2</span>}]; |
| 122 | |
| 123 | <span class="kw">proc</span> main*; |
| 124 | <span class="kw">var</span> i, total: <span class="ty">INT</span>; |
| 125 | <span class="kw">begin</span> |
| 126 | <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="ty">LEN</span>(Inner) - <span class="number">1</span> <span class="kw">do</span> |
| 127 | total := total + Inner[i].moons; |
| 128 | <span class="kw">if</span> Inner[i].moons = <span class="number">0</span> <span class="kw">then</span> Fmt.print(<span class="string">"{} has no moon\n"</span>, [Fmt.str(Inner[i].name)]); <span class="kw">end</span>; |
| 129 | <span class="kw">end</span>; |
| 130 | Fmt.print(<span class="string">"{} moons in all\n"</span>, [Fmt.int(total)]); |
| 131 | <span class="kw">end</span>; |
| 132 | |
| 133 | <span class="kw">end</span> Planets.</code></pre> |
| 134 | <pre class="output"><code>Mercury has no moon |
| 135 | Venus has no moon |
| 136 | 3 moons in all |
| 137 | </code></pre> |
| 138 | <p>Fields left out are zero, as in any literal: Mercury's <code>moons</code> is 0 and its <code>water</code> is <code>false</code>. A constant typed <code>[]Planet</code> takes its length from its literal, here 4, so a planet added to the list needs no count changed.</p> |
| 139 | <h2>Layout</h2> |
| 140 | <p>A record's layout is C's: each field at the next multiple of its own alignment, and the whole padded to a multiple of its largest alignment. <code>SIZE(T)</code> says how many bytes. A <code>packed record</code> has no padding, which is what a file format or a wire protocol wants.</p> |
| 141 | <div class="file"><span>Layout.raz</span></div><pre class="code"><code><span class="kw">module</span> Layout; |
| 142 | <span class="kw">import</span> Out; |
| 143 | |
| 144 | <span class="kw">type</span> |
| 145 | Loose = <span class="kw">record</span> a: <span class="ty">U8</span>; b: <span class="ty">U32</span>; c: <span class="ty">U8</span>; <span class="kw">end</span>; |
| 146 | Tight = <span class="kw">packed</span> <span class="kw">record</span> a: <span class="ty">U8</span>; b: <span class="ty">U32</span>; c: <span class="ty">U8</span>; <span class="kw">end</span>; |
| 147 | |
| 148 | <span class="kw">proc</span> main*; |
| 149 | <span class="kw">begin</span> |
| 150 | Out.str(<span class="string">"loose: "</span>); Out.int(<span class="ty">SIZE</span>(Loose)); Out.ln; <span class="comment">(* 12 *)</span> |
| 151 | Out.str(<span class="string">"tight: "</span>); Out.int(<span class="ty">SIZE</span>(Tight)); Out.ln; <span class="comment">(* 6 *)</span> |
| 152 | Out.str(<span class="string">"INT: "</span>); Out.int(<span class="ty">SIZE</span>(<span class="ty">INT</span>)); Out.ln; |
| 153 | Out.str(<span class="string">"[5]I16: "</span>); Out.int(<span class="ty">SIZE</span>([<span class="number">5</span>]<span class="ty">I16</span>)); Out.ln; |
| 154 | <span class="kw">end</span>; |
| 155 | |
| 156 | <span class="kw">end</span> Layout.</code></pre> |
| 157 | <pre class="output"><code>loose: 12 |
| 158 | tight: 6 |
| 159 | INT: 8 |
| 160 | [5]I16: 10 |
| 161 | </code></pre> |
| 162 | <p>A field of a packed record may sit at any address, and is read and written like any other; but it has no address of its own to take with <code>SYSTEM.ADR</code>.</p> |
| 163 | <h2>Bytes as numbers and records: views</h2> |
| 164 | <p>A file, a socket or a device hands a program bytes. A view reads and writes them as another type: <code>msg[3].(U32)</code> is the <code>U32</code> whose four bytes start at <code>msg[3]</code>, and <code>msg[0].(Head)</code> the <code>Head</code> whose bytes start at <code>msg[0]</code>.</p> |
| 165 | <div class="file"><span>Wire.raz</span></div><pre class="code"><code><span class="kw">module</span> Wire; |
| 166 | <span class="kw">import</span> Out; |
| 167 | |
| 168 | <span class="kw">type</span> Head = <span class="kw">packed</span> <span class="kw">record</span> magic: <span class="ty">U16</span>; kind: <span class="ty">U8</span>; len: <span class="ty">U32</span>; <span class="kw">end</span>; <span class="comment">(* 7 bytes, as sent *)</span> |
| 169 | |
| 170 | <span class="kw">var</span> msg: [<span class="number">32</span>]<span class="ty">U8</span>; |
| 171 | |
| 172 | <span class="kw">proc</span> main*; |
| 173 | <span class="kw">var</span> h: Head; i: <span class="ty">INT</span>; sum: <span class="ty">I64</span>; |
| 174 | <span class="kw">begin</span> |
| 175 | <span class="comment">(* what a socket might have handed us *)</span> |
| 176 | msg[<span class="number">0</span>] := <span class="number">52H</span>; msg[<span class="number">1</span>] := <span class="number">5AH</span>; |
| 177 | msg[<span class="number">2</span>] := <span class="number">1</span>; |
| 178 | msg[<span class="number">3</span>].(<span class="ty">U32</span>) := <span class="number">8</span>; |
| 179 | msg[<span class="number">7</span>].(<span class="ty">I32</span>) := <span class="number">100</span>; |
| 180 | msg[<span class="number">11</span>].(<span class="ty">I32</span>) := <span class="number">200</span>; |
| 181 | |
| 182 | h := msg[<span class="number">0</span>].(Head); |
| 183 | Out.hex(<span class="ty">U64</span>(h.magic), <span class="number">4</span>); Out.char(<span class="string">' '</span>); |
| 184 | Out.int(<span class="ty">I64</span>(h.kind)); Out.char(<span class="string">' '</span>); |
| 185 | Out.int(<span class="ty">I64</span>(h.len)); Out.ln; |
| 186 | sum := <span class="number">0</span>; |
| 187 | i := <span class="number">7</span>; |
| 188 | <span class="kw">while</span> i < <span class="number">7</span> + <span class="ty">INT</span>(h.len) <span class="kw">do</span> |
| 189 | sum := sum + <span class="ty">I64</span>(msg[i].(<span class="ty">I32</span>)); |
| 190 | i := i + <span class="number">4</span>; |
| 191 | <span class="kw">end</span>; |
| 192 | Out.int(sum); Out.ln; |
| 193 | <span class="kw">end</span>; |
| 194 | |
| 195 | <span class="kw">end</span> Wire.</code></pre> |
| 196 | <pre class="output"><code>5A52 1 8 |
| 197 | 300 |
| 198 | </code></pre> |
| 199 | <p>A view isn't a copy: it *is* those bytes, so <code>msg[3].(U32) := 8</code> writes <code>msg[3]</code> to <code>msg[6]</code>, and a view goes wherever a variable does, with a field after it, in <code>INC</code>, on the left of <code>:=</code>. Nor is it a conversion: <code>I64(x)</code> is a real's number, <code>x.(U64)</code> its bits. The magic number reads <code>5A52</code>, its bytes backwards, because x86-64 keeps a number's low byte first; a format with an order of its own is read with <code>Str.le</code> or <code>Str.be</code>.</p> |
| 200 | <p>A view of an element must end inside the array, and the program checks it as it checks an index:</p> |
| 201 | <div class="file"><span>Short.raz</span></div><pre class="code"><code><span class="kw">module</span> Short; |
| 202 | <span class="kw">import</span> Out; |
| 203 | |
| 204 | <span class="kw">var</span> msg: [<span class="number">10</span>]<span class="ty">U8</span>; |
| 205 | |
| 206 | <span class="kw">proc</span> main*; |
| 207 | <span class="kw">var</span> i: <span class="ty">INT</span>; |
| 208 | <span class="kw">begin</span> |
| 209 | i := <span class="number">0</span>; |
| 210 | <span class="kw">while</span> i < <span class="ty">LEN</span>(msg) <span class="kw">do</span> |
| 211 | Out.int(<span class="ty">I64</span>(msg[i].(<span class="ty">U32</span>))); Out.char(<span class="string">' '</span>); |
| 212 | i := i + <span class="number">4</span>; |
| 213 | <span class="kw">end</span>; |
| 214 | <span class="kw">end</span>; |
| 215 | |
| 216 | <span class="kw">end</span> Short.</code></pre> |
| 217 | <pre class="output"><code>0 0 <span class="err">Short.raz:11:25: a view at index 8 runs past a length of 10 |
| 218 | in Short.main at Short.raz:11:25: i = 8 |
| 219 | </span></code></pre> |
| 220 | <p>The type of a view is an integer, a real or a pointer, or a record or array made of them: types any bytes are a value of, which a <code>BOOL</code> or an enumeration isn't. A view may start at any byte, so, as a packed record's field, it has no address of its own.</p> |
| 221 | <h2>Pointers</h2> |
| 222 | <p><code>SYSTEM.ADR(x)</code> (a module that uses it says <code>import SYSTEM;</code>) takes the address of a variable, <code>^T</code> is a pointer to a <code>T</code>, and <code>p^</code> is what it points at. A pointer that points nowhere is <code>nil</code>, and following it stops the program with a message that names the place, so a forgotten check is found at once instead of corrupting memory.</p> |
| 223 | <div class="file"><span>Pointers.raz</span></div><pre class="code"><code><span class="kw">module</span> Pointers; |
| 224 | <span class="kw">import</span> Out; |
| 225 | <span class="kw">import</span> SYSTEM; |
| 226 | |
| 227 | <span class="kw">type</span> Counter = <span class="kw">record</span> hits: <span class="ty">INT</span>; name: []<span class="kw">const</span> <span class="ty">U8</span>; <span class="kw">end</span>; |
| 228 | |
| 229 | <span class="kw">proc</span> hit(c: ^Counter); |
| 230 | <span class="kw">begin</span> |
| 231 | <span class="kw">if</span> c = <span class="ty">nil</span> <span class="kw">then</span> <span class="kw">return</span>; <span class="kw">end</span>; |
| 232 | <span class="ty">INC</span>(c^.hits); |
| 233 | <span class="kw">end</span>; |
| 234 | |
| 235 | <span class="kw">proc</span> swap(a, b: ^<span class="ty">INT</span>); |
| 236 | <span class="kw">var</span> t: <span class="ty">INT</span>; |
| 237 | <span class="kw">begin</span> |
| 238 | t := a^; |
| 239 | a^ := b^; |
| 240 | b^ := t; |
| 241 | <span class="kw">end</span>; |
| 242 | |
| 243 | <span class="kw">proc</span> main*; |
| 244 | <span class="kw">var</span> c: Counter; x, y: <span class="ty">INT</span>; p: ^<span class="ty">INT</span>; |
| 245 | <span class="kw">begin</span> |
| 246 | c.name := <span class="string">"front door"</span>; |
| 247 | hit(SYSTEM.<span class="ty">ADR</span>(c)); hit(SYSTEM.<span class="ty">ADR</span>(c)); hit(<span class="ty">nil</span>); |
| 248 | Out.str(c.name); Out.str(<span class="string">": "</span>); Out.int(c.hits); Out.ln; |
| 249 | x := <span class="number">1</span>; y := <span class="number">2</span>; |
| 250 | swap(SYSTEM.<span class="ty">ADR</span>(x), SYSTEM.<span class="ty">ADR</span>(y)); |
| 251 | Out.int(x); Out.char(<span class="string">' '</span>); Out.int(y); Out.ln; |
| 252 | p := SYSTEM.<span class="ty">ADR</span>(x); |
| 253 | p^ := p^ + <span class="number">40</span>; |
| 254 | Out.int(x); Out.ln; |
| 255 | <span class="kw">end</span>; |
| 256 | |
| 257 | <span class="kw">end</span> Pointers.</code></pre> |
| 258 | <pre class="output"><code>front door: 2 |
| 259 | 2 1 |
| 260 | 42 |
| 261 | </code></pre> |
| 262 | <h2>Pointers that link</h2> |
| 263 | <p>A record may hold a pointer to its own type, which is how lists and trees are made. Memory for them comes from an allocator, which is the next lesson; here the nodes simply live in an array.</p> |
| 264 | <div class="file"><span>Chain.raz</span></div><pre class="code"><code><span class="kw">module</span> Chain; |
| 265 | <span class="kw">import</span> Out; |
| 266 | <span class="kw">import</span> SYSTEM; |
| 267 | |
| 268 | <span class="kw">type</span> Node = <span class="kw">record</span> next: ^Node; val: <span class="ty">INT</span>; <span class="kw">end</span>; |
| 269 | |
| 270 | <span class="kw">proc</span> main*; |
| 271 | <span class="kw">var</span> nodes: [<span class="number">4</span>]Node; head, n: ^Node; i: <span class="ty">INT</span>; |
| 272 | <span class="kw">begin</span> |
| 273 | head := <span class="ty">nil</span>; |
| 274 | <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">3</span> <span class="kw">do</span> |
| 275 | nodes[i].val := (i + <span class="number">1</span>) * <span class="number">10</span>; |
| 276 | nodes[i].next := head; <span class="comment">(* each new one in front *)</span> |
| 277 | head := SYSTEM.<span class="ty">ADR</span>(nodes[i]); |
| 278 | <span class="kw">end</span>; |
| 279 | n := head; |
| 280 | <span class="kw">while</span> n # <span class="ty">nil</span> <span class="kw">do</span> |
| 281 | Out.int(n^.val); Out.char(<span class="string">' '</span>); |
| 282 | n := n^.next; |
| 283 | <span class="kw">end</span>; |
| 284 | Out.ln; |
| 285 | <span class="kw">end</span>; |
| 286 | |
| 287 | <span class="kw">end</span> Chain.</code></pre> |
| 288 | <pre class="output"><code>40 30 20 10 |
| 289 | </code></pre> |
| 290 | <h2>A slice's pointer</h2> |
| 291 | <p>A slice is a pointer and a length, and a record can keep the two apart: an interpreter keeps a string so, and C takes one so. <code>LEN(s)</code> is the length. The pointer looks like <code>SYSTEM.ADR(s[0])</code>, the address of the first element, and that works until the slice is empty: then there is no first element, <code>s[0]</code> is out of range, and the program stops.</p> |
| 292 | <div class="file"><span>Empty.raz</span></div><pre class="code"><code><span class="kw">module</span> Empty; |
| 293 | <span class="kw">import</span> Out; |
| 294 | <span class="kw">import</span> SYSTEM; |
| 295 | |
| 296 | <span class="kw">type</span> Span = <span class="kw">record</span> p: ^<span class="ty">U8</span>; n: <span class="ty">INT</span>; <span class="kw">end</span>; |
| 297 | |
| 298 | <span class="kw">proc</span> spanOf(s: []<span class="kw">const</span> <span class="ty">U8</span>): Span; |
| 299 | <span class="kw">begin</span> |
| 300 | <span class="kw">return</span> {p: SYSTEM.<span class="ty">ADR</span>(s[<span class="number">0</span>]), n: <span class="ty">LEN</span>(s)}; |
| 301 | <span class="kw">end</span>; |
| 302 | |
| 303 | <span class="kw">proc</span> main*; |
| 304 | <span class="kw">var</span> a, b: Span; |
| 305 | <span class="kw">begin</span> |
| 306 | a := spanOf(<span class="string">"one"</span>); |
| 307 | Out.int(a.n); Out.ln; |
| 308 | b := spanOf(<span class="string">""</span>); |
| 309 | Out.int(b.n); Out.ln; |
| 310 | <span class="kw">end</span>; |
| 311 | |
| 312 | <span class="kw">end</span> Empty.</code></pre> |
| 313 | <pre class="output"><code><span class="err">Empty.raz:9:27: error: s[0] is read through a []const slice, and can't be changed |
| 314 | 9 | return {p: SYSTEM.ADR(s[0]), n: LEN(s)}; |
| 315 | | ^^^^ |
| 316 | help: a []const slice's elements are only read: write them through a []T |
| 317 | raz: 1 error |
| 318 | </span></code></pre> |
| 319 | <p><code>SYSTEM.PTR(s)</code> is the pointer itself, a <code>^U8</code> for a <code>[]U8</code>, with no check. For a slice that isn't empty it is <code>SYSTEM.ADR(s[0])</code>; for an empty one it is what the slice holds, <code>nil</code> for one never set. So nothing needs testing first, and <code>sp.p[0..sp.n]</code> makes the slice again, empty when <code>sp.n</code> is 0, whatever <code>sp.p</code> is:</p> |
| 320 | <div class="file"><span>Spans.raz</span></div><pre class="code"><code><span class="kw">module</span> Spans; |
| 321 | <span class="kw">import</span> Out, SYSTEM; |
| 322 | |
| 323 | <span class="kw">type</span> Span = <span class="kw">record</span> p: ^<span class="ty">U8</span>; n: <span class="ty">INT</span>; <span class="kw">end</span>; |
| 324 | |
| 325 | <span class="kw">proc</span> spanOf(s: []<span class="kw">const</span> <span class="ty">U8</span>): Span; |
| 326 | <span class="kw">begin</span> |
| 327 | <span class="kw">return</span> {p: SYSTEM.PTR(s), n: <span class="ty">LEN</span>(s)}; |
| 328 | <span class="kw">end</span>; |
| 329 | |
| 330 | <span class="kw">proc</span> text(sp: Span): []<span class="ty">U8</span>; |
| 331 | <span class="kw">begin</span> |
| 332 | <span class="kw">return</span> sp.p[<span class="number">0</span>..sp.n]; |
| 333 | <span class="kw">end</span>; |
| 334 | |
| 335 | <span class="kw">proc</span> main*; |
| 336 | <span class="kw">var</span> words: [<span class="number">3</span>]Span; i: <span class="ty">INT</span>; |
| 337 | <span class="kw">begin</span> |
| 338 | words := [spanOf(<span class="string">"one"</span>), spanOf(<span class="string">""</span>), spanOf(<span class="string">"three"</span>)]; |
| 339 | <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">2</span> <span class="kw">do</span> |
| 340 | Out.int(words[i].n); Out.char(<span class="string">' '</span>); |
| 341 | Out.str(text(words[i])); Out.ln; |
| 342 | <span class="kw">end</span>; |
| 343 | <span class="kw">end</span>; |
| 344 | |
| 345 | <span class="kw">end</span> Spans.</code></pre> |
| 346 | <pre class="output"><code>3 one |
| 347 | 0 |
| 348 | 5 three |
| 349 | </code></pre> |
| 350 | <p>Of an array, <code>SYSTEM.PTR(a)</code> is the address of its first element, a <code>^T</code> where <code>SYSTEM.ADR(a)</code> is a <code>^[n]T</code>. Reading through the pointer is as unchecked as through any pointer: the length is yours to keep with it. The compiler says so when the two are mixed up: a slice given where a pointer is wanted is an error whose help says to write <code>PTR(s)</code> and <code>LEN(s)</code>, and a pointer given where a slice is, <code>p[0..n]</code>.</p><p class="tryit">Save a program under its module’s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="slices.html">← Arrays, slices and strings</a><a href="index.html">Contents</a><a class="next" href="variants.html">Enumerations, sets, variants and match →</a></div></article></div> |
| 351 | </main> |
| 352 | <footer>raz is under the ISC license. <a href="../source/index.html">Source</a></footer> |
| 353 | </body> |
| 354 | </html> |