raz / site / public / tour / defer.html

187 lines · 10.7 KiB · raw · history · last change de634aad

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 &middot; 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">&quot;in &quot;</span>); Out.int(n); Out.ln;
48 <span class="kw">defer</span> Out.str(<span class="string">&quot;out\n&quot;</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">&quot;done\n&quot;</span>);
51 Out.str(<span class="string">&quot;working\n&quot;</span>);
52<span class="kw">end</span>;
53
54<span class="comment">(* the lines of the file at path, or -1 if it can&#x27;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 &lt; <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 &gt; <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">&#x27;\n&#x27;</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">&quot;/dev/null&quot;</span>)); Out.ln;
77 Out.int(lines(<span class="string">&quot;/no/such/file&quot;</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
82working
83done
84out
85in 2
86out
870
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">&quot;1 &quot;</span>);
107 <span class="kw">defer</span> Out.str(<span class="string">&quot;2 &quot;</span>);
108 <span class="kw">defer</span> Out.str(<span class="string">&quot;3 &quot;</span>);
109 Out.str(<span class="string">&quot;body: &quot;</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">&#x27; &#x27;</span>); Out.int(b); Out.char(<span class="string">&#x27; &#x27;</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
124body: 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">&quot;pass &quot;</span>); Out.int(i);
136 <span class="kw">defer</span> Out.str(<span class="string">&quot; done\n&quot;</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">&quot; ...&quot;</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
144pass 2 ... done
145pass 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 &lt; <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">&quot;n is &quot;</span>);
160 <span class="kw">if</span> n &gt; <span class="number">9</span> <span class="kw">then</span>
161 Buf.str(b, <span class="string">&quot;large&quot;</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">&quot;small&quot;</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">&quot;rejected\n&quot;</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
179n is large
180rejected
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&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="errors.html">&larr; Errors</a><a href="index.html">Contents</a><a class="next" href="checks.html">When it goes wrong &rarr;</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>