| 1 | title: Modules |
| 2 | part: Raz |
| 3 | summary: Exports, renaming, the order bodies run in, and what the compiler refuses. |
| 4 | --- |
| 5 | A program is a set of modules, one to a file, and a module's interface is everything it marks with `*`. Everything else is private to it. Here is one, saved as `Greet.raz`: |
| 6 | |
| 7 | ```raz Greet |
| 8 | ``` |
| 9 | |
| 10 | and a program beside it that uses it: |
| 11 | |
| 12 | ```raz Twice run |
| 13 | ``` |
| 14 | |
| 15 | A name with `*` is exported; any other is the module's own. `G := Greet` imports `Greet` under a shorter name, so its exports are `G.hello` and `G.count`. Another module may read `G.count`, but only `Greet` may change it. |
| 16 | |
| 17 | ## Where modules are found |
| 18 | |
| 19 | The compiler looks for `Greet.raz` in the main module's directory, then in each directory given with `-I`, then in the library. So a project is a directory of modules, and `raz Main.raz` finds all of them. The library's own names (`Out`, `Str`, `Map`, ...) are kept for it. |
| 20 | |
| 21 | ## Bodies run first, in order |
| 22 | |
| 23 | The statements after a module's last `begin` are its body. It runs once, before `main`, after the bodies of the modules it imports, and once however many modules import it. This is how a module sets itself up: no `init` call to forget. |
| 24 | |
| 25 | ```raz Base |
| 26 | module Base; |
| 27 | import Out; |
| 28 | |
| 29 | var ready*: BOOL; |
| 30 | |
| 31 | begin |
| 32 | Out.str("Base starts\n"); |
| 33 | ready := true; |
| 34 | end Base. |
| 35 | ``` |
| 36 | |
| 37 | ```raz Layer |
| 38 | module Layer; |
| 39 | import Out, Base; |
| 40 | |
| 41 | const Name* = "Layer"; |
| 42 | |
| 43 | begin |
| 44 | Out.str("Layer starts, Base is ready: "); |
| 45 | if Base.ready then Out.str("yes\n"); else Out.str("no\n"); end; |
| 46 | end Layer. |
| 47 | ``` |
| 48 | |
| 49 | ```raz Order run |
| 50 | module Order; |
| 51 | import Out, Layer, Base; |
| 52 | |
| 53 | proc main*; |
| 54 | begin |
| 55 | Out.str("main, after "); Out.str(Layer.Name); Out.char(' '); |
| 56 | if Base.ready then Out.str("and Base\n"); end; |
| 57 | end; |
| 58 | |
| 59 | end Order. |
| 60 | ``` |
| 61 | |
| 62 | `Base` is imported twice, but starts once, before `Layer`, which needs it. A module with no body doesn't need a `begin` at all. |
| 63 | |
| 64 | ## Exporting more than procedures |
| 65 | |
| 66 | 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. |
| 67 | |
| 68 | ```raz Shape |
| 69 | module Shape; |
| 70 | |
| 71 | const Sides* = 4; |
| 72 | |
| 73 | type |
| 74 | Square* = record |
| 75 | side*: INT; (* exported: a user may set it *) |
| 76 | id: INT; (* the module's own *) |
| 77 | end; |
| 78 | |
| 79 | var made: INT; (* only Shape may read or write it *) |
| 80 | |
| 81 | (* a square of that side, numbered in the order they are made *) |
| 82 | proc make*(side: INT): Square; |
| 83 | var s: Square; |
| 84 | begin |
| 85 | INC(made); |
| 86 | s.side := side; |
| 87 | s.id := made; |
| 88 | return s; |
| 89 | end; |
| 90 | |
| 91 | proc area*(s: Square): INT; |
| 92 | begin |
| 93 | return s.side * s.side; |
| 94 | end; |
| 95 | |
| 96 | proc count*: INT; |
| 97 | begin |
| 98 | return made; |
| 99 | end; |
| 100 | |
| 101 | end Shape. |
| 102 | ``` |
| 103 | |
| 104 | ```raz Squares run |
| 105 | module Squares; |
| 106 | import Out, Shape; |
| 107 | |
| 108 | proc main*; |
| 109 | var a, b: Shape.Square; |
| 110 | begin |
| 111 | a := Shape.make(3); |
| 112 | b := Shape.make(5); |
| 113 | Out.int(Shape.area(a) + Shape.area(b)); Out.ln; |
| 114 | Out.int(Shape.count()); Out.str(" made, "); Out.int(Shape.Sides); |
| 115 | Out.str(" sides each\n"); |
| 116 | end; |
| 117 | |
| 118 | end Squares. |
| 119 | ``` |
| 120 | |
| 121 | ## What the compiler refuses |
| 122 | |
| 123 | Reaching past an interface is an error, and the error says where. |
| 124 | |
| 125 | ```raz Peek |
| 126 | module Peek; |
| 127 | import Shape; |
| 128 | |
| 129 | proc main*; |
| 130 | var s: Shape.Square; |
| 131 | begin |
| 132 | s := Shape.make(2); |
| 133 | s.id := 7; |
| 134 | Shape.made := 0; |
| 135 | end; |
| 136 | |
| 137 | end Peek. |
| 138 | ``` |
| 139 | |
| 140 | ```sh |
| 141 | $ raz Peek.raz |
| 142 | ``` |
| 143 | |
| 144 | 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. |