| 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>Modules - 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="Modules - 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" aria-current="page">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">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">Raz · lesson 2 of 15</p><h1>Modules</h1> |
| 42 | <p>A program is a set of modules, one to a file, and a module's interface is everything it marks with <code>*</code>. Everything else is private to it. Here is one, saved as <code>Greet.raz</code>:</p> |
| 43 | <div class="file"><span>Greet.raz</span></div><pre class="code"><code><span class="kw">module</span> Greet; |
| 44 | <span class="kw">import</span> Out; |
| 45 | |
| 46 | <span class="kw">var</span> count*: <span class="ty">INT</span>; <span class="comment">(* the greetings so far *)</span> |
| 47 | |
| 48 | <span class="kw">proc</span> hello*(name: []<span class="kw">const</span> <span class="ty">U8</span>); |
| 49 | <span class="kw">begin</span> |
| 50 | Out.str(<span class="string">"hello, "</span>); |
| 51 | Out.str(name); |
| 52 | Out.ln; |
| 53 | <span class="ty">INC</span>(count); |
| 54 | <span class="kw">end</span>; |
| 55 | |
| 56 | <span class="kw">begin</span> |
| 57 | Out.str(<span class="string">"Greet is ready\n"</span>); |
| 58 | <span class="kw">end</span> Greet.</code></pre> |
| 59 | <p>and a program beside it that uses it:</p> |
| 60 | <div class="file"><span>Twice.raz</span></div><pre class="code"><code><span class="kw">module</span> Twice; |
| 61 | <span class="kw">import</span> Out, G := Greet; |
| 62 | |
| 63 | <span class="kw">proc</span> main*; |
| 64 | <span class="kw">begin</span> |
| 65 | G.hello(<span class="string">"world"</span>); |
| 66 | G.hello(<span class="string">"raz"</span>); |
| 67 | Out.int(G.count); |
| 68 | Out.str(<span class="string">" greetings\n"</span>); |
| 69 | <span class="kw">end</span>; |
| 70 | |
| 71 | <span class="kw">end</span> Twice.</code></pre> |
| 72 | <pre class="output"><code>Greet is ready |
| 73 | hello, world |
| 74 | hello, raz |
| 75 | 2 greetings |
| 76 | </code></pre> |
| 77 | <p>A name with <code>*</code> is exported; any other is the module's own. <code>G := Greet</code> imports <code>Greet</code> under a shorter name, so its exports are <code>G.hello</code> and <code>G.count</code>. Another module may read <code>G.count</code>, but only <code>Greet</code> may change it.</p> |
| 78 | <h2>Where modules are found</h2> |
| 79 | <p>The compiler looks for <code>Greet.raz</code> in the main module's directory, then in each directory given with <code>-I</code>, then in the library. So a project is a directory of modules, and <code>raz Main.raz</code> finds all of them. The library's own names (<code>Out</code>, <code>Str</code>, <code>Map</code>, ...) are kept for it.</p> |
| 80 | <h2>Bodies run first, in order</h2> |
| 81 | <p>The statements after a module's last <code>begin</code> are its body. It runs once, before <code>main</code>, after the bodies of the modules it imports, and once however many modules import it. This is how a module sets itself up: no <code>init</code> call to forget.</p> |
| 82 | <div class="file"><span>Base.raz</span></div><pre class="code"><code><span class="kw">module</span> Base; |
| 83 | <span class="kw">import</span> Out; |
| 84 | |
| 85 | <span class="kw">var</span> ready*: <span class="ty">BOOL</span>; |
| 86 | |
| 87 | <span class="kw">begin</span> |
| 88 | Out.str(<span class="string">"Base starts\n"</span>); |
| 89 | ready := <span class="ty">true</span>; |
| 90 | <span class="kw">end</span> Base.</code></pre> |
| 91 | <div class="file"><span>Layer.raz</span></div><pre class="code"><code><span class="kw">module</span> Layer; |
| 92 | <span class="kw">import</span> Out, Base; |
| 93 | |
| 94 | <span class="kw">const</span> Name* = <span class="string">"Layer"</span>; |
| 95 | |
| 96 | <span class="kw">begin</span> |
| 97 | Out.str(<span class="string">"Layer starts, Base is ready: "</span>); |
| 98 | <span class="kw">if</span> Base.ready <span class="kw">then</span> Out.str(<span class="string">"yes\n"</span>); <span class="kw">else</span> Out.str(<span class="string">"no\n"</span>); <span class="kw">end</span>; |
| 99 | <span class="kw">end</span> Layer.</code></pre> |
| 100 | <div class="file"><span>Order.raz</span></div><pre class="code"><code><span class="kw">module</span> Order; |
| 101 | <span class="kw">import</span> Out, Layer, Base; |
| 102 | |
| 103 | <span class="kw">proc</span> main*; |
| 104 | <span class="kw">begin</span> |
| 105 | Out.str(<span class="string">"main, after "</span>); Out.str(Layer.Name); Out.char(<span class="string">' '</span>); |
| 106 | <span class="kw">if</span> Base.ready <span class="kw">then</span> Out.str(<span class="string">"and Base\n"</span>); <span class="kw">end</span>; |
| 107 | <span class="kw">end</span>; |
| 108 | |
| 109 | <span class="kw">end</span> Order.</code></pre> |
| 110 | <pre class="output"><code>Base starts |
| 111 | Layer starts, Base is ready: yes |
| 112 | main, after Layer and Base |
| 113 | </code></pre> |
| 114 | <p><code>Base</code> is imported twice, but starts once, before <code>Layer</code>, which needs it. A module with no body doesn't need a <code>begin</code> at all.</p> |
| 115 | <h2>Exporting more than procedures</h2> |
| 116 | <p>A module exports constants, types, variables and procedures alike. A record type's exported fields are marked too; the rest are the module's own.</p> |
| 117 | <div class="file"><span>Shape.raz</span></div><pre class="code"><code><span class="kw">module</span> Shape; |
| 118 | |
| 119 | <span class="kw">const</span> Sides* = <span class="number">4</span>; |
| 120 | |
| 121 | <span class="kw">type</span> |
| 122 | Square* = <span class="kw">record</span> |
| 123 | side*: <span class="ty">INT</span>; <span class="comment">(* exported: a user may set it *)</span> |
| 124 | id: <span class="ty">INT</span>; <span class="comment">(* the module's own *)</span> |
| 125 | <span class="kw">end</span>; |
| 126 | |
| 127 | <span class="kw">var</span> made: <span class="ty">INT</span>; <span class="comment">(* only Shape may read or write it *)</span> |
| 128 | |
| 129 | <span class="comment">(* a square of that side, numbered in the order they are made *)</span> |
| 130 | <span class="kw">proc</span> make*(side: <span class="ty">INT</span>): Square; |
| 131 | <span class="kw">var</span> s: Square; |
| 132 | <span class="kw">begin</span> |
| 133 | <span class="ty">INC</span>(made); |
| 134 | s.side := side; |
| 135 | s.id := made; |
| 136 | <span class="kw">return</span> s; |
| 137 | <span class="kw">end</span>; |
| 138 | |
| 139 | <span class="kw">proc</span> area*(s: Square): <span class="ty">INT</span>; |
| 140 | <span class="kw">begin</span> |
| 141 | <span class="kw">return</span> s.side * s.side; |
| 142 | <span class="kw">end</span>; |
| 143 | |
| 144 | <span class="kw">proc</span> count*: <span class="ty">INT</span>; |
| 145 | <span class="kw">begin</span> |
| 146 | <span class="kw">return</span> made; |
| 147 | <span class="kw">end</span>; |
| 148 | |
| 149 | <span class="kw">end</span> Shape.</code></pre> |
| 150 | <div class="file"><span>Squares.raz</span></div><pre class="code"><code><span class="kw">module</span> Squares; |
| 151 | <span class="kw">import</span> Out, Shape; |
| 152 | |
| 153 | <span class="kw">proc</span> main*; |
| 154 | <span class="kw">var</span> a, b: Shape.Square; |
| 155 | <span class="kw">begin</span> |
| 156 | a := Shape.make(<span class="number">3</span>); |
| 157 | b := Shape.make(<span class="number">5</span>); |
| 158 | Out.int(Shape.area(a) + Shape.area(b)); Out.ln; |
| 159 | Out.int(Shape.count()); Out.str(<span class="string">" made, "</span>); Out.int(Shape.Sides); |
| 160 | Out.str(<span class="string">" sides each\n"</span>); |
| 161 | <span class="kw">end</span>; |
| 162 | |
| 163 | <span class="kw">end</span> Squares.</code></pre> |
| 164 | <pre class="output"><code>34 |
| 165 | 2 made, 4 sides each |
| 166 | </code></pre> |
| 167 | <h2>What the compiler refuses</h2> |
| 168 | <p>Reaching past an interface is an error, and the error says where.</p> |
| 169 | <div class="file"><span>Peek.raz</span></div><pre class="code"><code><span class="kw">module</span> Peek; |
| 170 | <span class="kw">import</span> Shape; |
| 171 | |
| 172 | <span class="kw">proc</span> main*; |
| 173 | <span class="kw">var</span> s: Shape.Square; |
| 174 | <span class="kw">begin</span> |
| 175 | s := Shape.make(<span class="number">2</span>); |
| 176 | s.id := <span class="number">7</span>; |
| 177 | Shape.made := <span class="number">0</span>; |
| 178 | <span class="kw">end</span>; |
| 179 | |
| 180 | <span class="kw">end</span> Peek.</code></pre> |
| 181 | <pre class="output term"><code><span class="cmd">$ raz Peek.raz</span> |
| 182 | Peek.raz:8:7: error: field id of Shape.Square isn't exported |
| 183 | 8 | s.id := 7; |
| 184 | | ^^ |
| 185 | ./Shape.raz:8:9: note: id is declared here, without a * |
| 186 | 8 | id: INT; (* the module's own *) |
| 187 | | ^^ |
| 188 | Peek.raz:9:11: error: made isn't exported by Shape |
| 189 | 9 | Shape.made := 0; |
| 190 | | ^^^^ |
| 191 | ./Shape.raz:11:5: note: made is declared here, without a * |
| 192 | 11 | var made: INT; (* only Shape may read or write it *) |
| 193 | | ^^^^ |
| 194 | raz: 2 errors</code></pre> |
| 195 | <p>The compiler goes on to find the others, up to 20, and says what it can: where a name was declared, or a name spelled almost alike. A module with errors writes no executable.</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="hello.html">← Hello, world</a><a href="index.html">Contents</a><a class="next" href="integers.html">Integers →</a></div></article></div> |
| 196 | </main> |
| 197 | <footer>raz is under the ISC license. <a href="../source/index.html">Source</a></footer> |
| 198 | </body> |
| 199 | </html> |