raz / site / content / tour / 02-modules.txt

144 lines · 3.2 KiB · raw · history · last change ddd82658

1title: Modules
2part: Raz
3summary: Exports, renaming, the order bodies run in, and what the compiler refuses.
4---
5A 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
10and a program beside it that uses it:
11
12```raz Twice run
13```
14
15A 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
19The 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
23The 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
26module Base;
27import Out;
28
29var ready*: BOOL;
30
31begin
32 Out.str("Base starts\n");
33 ready := true;
34end Base.
35```
36
37```raz Layer
38module Layer;
39import Out, Base;
40
41const Name* = "Layer";
42
43begin
44 Out.str("Layer starts, Base is ready: ");
45 if Base.ready then Out.str("yes\n"); else Out.str("no\n"); end;
46end Layer.
47```
48
49```raz Order run
50module Order;
51import Out, Layer, Base;
52
53proc main*;
54begin
55 Out.str("main, after "); Out.str(Layer.Name); Out.char(' ');
56 if Base.ready then Out.str("and Base\n"); end;
57end;
58
59end 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
66A 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
69module Shape;
70
71const Sides* = 4;
72
73type
74 Square* = record
75 side*: INT; (* exported: a user may set it *)
76 id: INT; (* the module's own *)
77 end;
78
79var made: INT; (* only Shape may read or write it *)
80
81(* a square of that side, numbered in the order they are made *)
82proc make*(side: INT): Square;
83var s: Square;
84begin
85 INC(made);
86 s.side := side;
87 s.id := made;
88 return s;
89end;
90
91proc area*(s: Square): INT;
92begin
93 return s.side * s.side;
94end;
95
96proc count*: INT;
97begin
98 return made;
99end;
100
101end Shape.
102```
103
104```raz Squares run
105module Squares;
106import Out, Shape;
107
108proc main*;
109var a, b: Shape.Square;
110begin
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");
116end;
117
118end Squares.
119```
120
121## What the compiler refuses
122
123Reaching past an interface is an error, and the error says where.
124
125```raz Peek
126module Peek;
127import Shape;
128
129proc main*;
130var s: Shape.Square;
131begin
132 s := Shape.make(2);
133 s.id := 7;
134 Shape.made := 0;
135end;
136
137end Peek.
138```
139
140```sh
141$ raz Peek.raz
142```
143
144The 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.