| 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>Defer - 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="Defer - 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">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" aria-current="page">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">Go · lesson 11 of 15</p><h1>Defer</h1> |
| 42 | <div class="file"><span>Defers.raz</span></div><pre class="code"><code><span class="kw">module</span> Defers; |
| 43 | <span class="kw">import</span> Out, Sys; |
| 44 | |
| 45 | <span class="kw">proc</span> step(n: <span class="ty">INT</span>); |
| 46 | <span class="kw">begin</span> |
| 47 | Out.str(<span class="string">"in "</span>); Out.int(n); Out.ln; |
| 48 | <span class="kw">defer</span> Out.str(<span class="string">"out\n"</span>); |
| 49 | <span class="kw">if</span> n = <span class="number">2</span> <span class="kw">then</span> <span class="kw">return</span>; <span class="kw">end</span>; |
| 50 | <span class="kw">defer</span> Out.str(<span class="string">"done\n"</span>); |
| 51 | Out.str(<span class="string">"working\n"</span>); |
| 52 | <span class="kw">end</span>; |
| 53 | |
| 54 | <span class="comment">(* the lines of the file at path, or -1 if it can't be read *)</span> |
| 55 | <span class="kw">proc</span> lines(path: []<span class="kw">const</span> <span class="ty">U8</span>): <span class="ty">INT</span>; |
| 56 | <span class="kw">var</span> fd, n, i, count: <span class="ty">INT</span>; buf: [<span class="number">4096</span>]<span class="ty">U8</span>; |
| 57 | <span class="kw">begin</span> |
| 58 | fd := Sys.openRead(path); |
| 59 | <span class="kw">if</span> fd < <span class="number">0</span> <span class="kw">then</span> <span class="kw">return</span> -<span class="number">1</span>; <span class="kw">end</span>; |
| 60 | <span class="kw">defer</span> Sys.close(fd); |
| 61 | count := <span class="number">0</span>; |
| 62 | n := Sys.read(fd, buf); |
| 63 | <span class="kw">while</span> n > <span class="number">0</span> <span class="kw">do</span> |
| 64 | <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> n - <span class="number">1</span> <span class="kw">do</span> |
| 65 | <span class="kw">if</span> buf[i] = <span class="string">'\n'</span> <span class="kw">then</span> <span class="ty">INC</span>(count); <span class="kw">end</span>; |
| 66 | <span class="kw">end</span>; |
| 67 | n := Sys.read(fd, buf); |
| 68 | <span class="kw">end</span>; |
| 69 | <span class="kw">return</span> count; |
| 70 | <span class="kw">end</span>; |
| 71 | |
| 72 | <span class="kw">proc</span> main*; |
| 73 | <span class="kw">begin</span> |
| 74 | step(<span class="number">1</span>); |
| 75 | step(<span class="number">2</span>); |
| 76 | Out.int(lines(<span class="string">"/dev/null"</span>)); Out.ln; |
| 77 | Out.int(lines(<span class="string">"/no/such/file"</span>)); Out.ln; |
| 78 | <span class="kw">end</span>; |
| 79 | |
| 80 | <span class="kw">end</span> Defers.</code></pre> |
| 81 | <pre class="output"><code>in 1 |
| 82 | working |
| 83 | done |
| 84 | out |
| 85 | in 2 |
| 86 | out |
| 87 | 0 |
| 88 | -1 |
| 89 | </code></pre> |
| 90 | <p><code>defer s;</code> runs <code>s</code> when the program leaves the statements the <code>defer</code> stands in: at their end, or by a <code>return</code>, a <code>break</code> or a <code>continue</code>. Deferred statements run the last first, and only those whose <code>defer</code> was reached. So a file is closed beside the line that opens it, however the procedure ends.</p> |
| 91 | <h2>The order, and a <code>return</code> value</h2> |
| 92 | <p>A <code>return e;</code> evaluates <code>e</code> before the deferred statements run, so the value is what it was at the return.</p> |
| 93 | <div class="file"><span>Order.raz</span></div><pre class="code"><code><span class="kw">module</span> Order; |
| 94 | <span class="kw">import</span> Out; |
| 95 | |
| 96 | <span class="kw">var</span> counter: <span class="ty">INT</span>; |
| 97 | |
| 98 | <span class="kw">proc</span> next(): <span class="ty">INT</span>; |
| 99 | <span class="kw">begin</span> |
| 100 | <span class="kw">defer</span> <span class="ty">INC</span>(counter); |
| 101 | <span class="kw">return</span> counter; <span class="comment">(* the old value is returned; then counter grows *)</span> |
| 102 | <span class="kw">end</span>; |
| 103 | |
| 104 | <span class="kw">proc</span> nest; |
| 105 | <span class="kw">begin</span> |
| 106 | <span class="kw">defer</span> Out.str(<span class="string">"1 "</span>); |
| 107 | <span class="kw">defer</span> Out.str(<span class="string">"2 "</span>); |
| 108 | <span class="kw">defer</span> Out.str(<span class="string">"3 "</span>); |
| 109 | Out.str(<span class="string">"body: "</span>); |
| 110 | <span class="kw">end</span>; |
| 111 | |
| 112 | <span class="kw">proc</span> main*; |
| 113 | <span class="kw">var</span> a, b: <span class="ty">INT</span>; |
| 114 | <span class="kw">begin</span> |
| 115 | a := next(); |
| 116 | b := next(); |
| 117 | Out.int(a); Out.char(<span class="string">' '</span>); Out.int(b); Out.char(<span class="string">' '</span>); Out.int(counter); Out.ln; |
| 118 | nest; |
| 119 | Out.ln; |
| 120 | <span class="kw">end</span>; |
| 121 | |
| 122 | <span class="kw">end</span> Order.</code></pre> |
| 123 | <pre class="output"><code>0 1 2 |
| 124 | body: 3 2 1 |
| 125 | </code></pre> |
| 126 | <h2>In a loop</h2> |
| 127 | <p>A <code>defer</code> stands in the statement list that holds it, so in a loop body it runs at the end of each pass, and on a <code>break</code> or a <code>continue</code>.</p> |
| 128 | <div class="file"><span>Passes.raz</span></div><pre class="code"><code><span class="kw">module</span> Passes; |
| 129 | <span class="kw">import</span> Out; |
| 130 | |
| 131 | <span class="kw">proc</span> main*; |
| 132 | <span class="kw">var</span> i: <span class="ty">INT</span>; |
| 133 | <span class="kw">begin</span> |
| 134 | <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> <span class="number">4</span> <span class="kw">do</span> |
| 135 | Out.str(<span class="string">"pass "</span>); Out.int(i); |
| 136 | <span class="kw">defer</span> Out.str(<span class="string">" done\n"</span>); |
| 137 | <span class="kw">if</span> i = <span class="number">3</span> <span class="kw">then</span> <span class="kw">break</span>; <span class="kw">end</span>; |
| 138 | Out.str(<span class="string">" ..."</span>); |
| 139 | <span class="kw">end</span>; |
| 140 | <span class="kw">end</span>; |
| 141 | |
| 142 | <span class="kw">end</span> Passes.</code></pre> |
| 143 | <pre class="output"><code>pass 1 ... done |
| 144 | pass 2 ... done |
| 145 | pass 3 done |
| 146 | </code></pre> |
| 147 | <h2>Giving back what was taken</h2> |
| 148 | <p>The point is that acquiring and releasing sit next to each other, so a new early <code>return</code> added later can't leak. Here an arena and a buffer are both released on every path.</p> |
| 149 | <div class="file"><span>Report.raz</span></div><pre class="code"><code><span class="kw">module</span> Report; |
| 150 | <span class="kw">import</span> Out, Buf, Heap; |
| 151 | |
| 152 | <span class="comment">(* a line, built in a buffer; false (nothing written) if n is negative *)</span> |
| 153 | <span class="kw">proc</span> line(n: <span class="ty">INT</span>): <span class="ty">BOOL</span>; |
| 154 | <span class="kw">var</span> b: Buf.Buf; |
| 155 | <span class="kw">begin</span> |
| 156 | <span class="kw">if</span> n < <span class="number">0</span> <span class="kw">then</span> <span class="kw">return</span> <span class="ty">false</span>; <span class="kw">end</span>; |
| 157 | Buf.init(b, Heap.heap); |
| 158 | <span class="kw">defer</span> Buf.done(b); <span class="comment">(* freed on every way out below *)</span> |
| 159 | Buf.str(b, <span class="string">"n is "</span>); |
| 160 | <span class="kw">if</span> n > <span class="number">9</span> <span class="kw">then</span> |
| 161 | Buf.str(b, <span class="string">"large"</span>); |
| 162 | Out.str(Buf.bytes(b)); Out.ln; |
| 163 | <span class="kw">return</span> <span class="ty">true</span>; |
| 164 | <span class="kw">end</span>; |
| 165 | Buf.str(b, <span class="string">"small"</span>); |
| 166 | Out.str(Buf.bytes(b)); Out.ln; |
| 167 | <span class="kw">return</span> <span class="ty">true</span>; |
| 168 | <span class="kw">end</span>; |
| 169 | |
| 170 | <span class="kw">proc</span> main*; |
| 171 | <span class="kw">begin</span> |
| 172 | _ := line(<span class="number">3</span>); |
| 173 | _ := line(<span class="number">30</span>); |
| 174 | <span class="kw">if</span> <span class="kw">not</span> line(-<span class="number">1</span>) <span class="kw">then</span> Out.str(<span class="string">"rejected\n"</span>); <span class="kw">end</span>; |
| 175 | <span class="kw">end</span>; |
| 176 | |
| 177 | <span class="kw">end</span> Report.</code></pre> |
| 178 | <pre class="output"><code>n is small |
| 179 | n is large |
| 180 | rejected |
| 181 | </code></pre> |
| 182 | <h2>What defer does not do</h2> |
| 183 | <p>A deferred statement can't contain a <code>return</code>, <code>break</code> or <code>defer</code> that would leave it. A failed check, <code>Rt.exit</code> and <code>Sys.exit</code> end the program without running the deferred statements still pending, so deferred cleanup is for the program's resources, not for what must happen at exit; that is what <code>Rt.atExit</code> is for.</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="errors.html">← Errors</a><a href="index.html">Contents</a><a class="next" href="checks.html">When it goes wrong →</a></div></article></div> |
| 184 | </main> |
| 185 | <footer>raz is under the ISC license. <a href="../source/index.html">Source</a></footer> |
| 186 | </body> |
| 187 | </html> |