Raz · lesson 2 of 15

Modules

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:

Greet.raz
module Greet;
import Out;

var count*: INT;    (* the greetings so far *)

proc hello*(name: []U8);
begin
    Out.str("hello, ");
    Out.str(name);
    Out.ln;
    INC(count);
end;

begin
    Out.str("Greet is ready\n");
end Greet.

and a program beside it that uses it:

Twice.raz
module Twice;
import Out, G := Greet;

proc main*;
begin
    G.hello("world");
    G.hello("raz");
    Out.int(G.count);
    Out.str(" greetings\n");
end;

end Twice.
Greet is ready
hello, world
hello, raz
2 greetings

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.

Where modules are found

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.

Bodies run first, in order

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.

Base.raz
module Base;
import Out;

var ready*: BOOL;

begin
    Out.str("Base starts\n");
    ready := true;
end Base.
Layer.raz
module Layer;
import Out, Base;

const Name* = "Layer";

begin
    Out.str("Layer starts, Base is ready: ");
    if Base.ready then Out.str("yes\n"); else Out.str("no\n"); end;
end Layer.
Order.raz
module Order;
import Out, Layer, Base;

proc main*;
begin
    Out.str("main, after "); Out.str(Layer.Name); Out.char(' ');
    if Base.ready then Out.str("and Base\n"); end;
end;

end Order.
Base starts
Layer starts, Base is ready: yes
main, after Layer and Base

Base is imported twice, but starts once, before Layer, which needs it. A module with no body doesn't need a begin at all.

Exporting more than procedures

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.

Shape.raz
module Shape;

const Sides* = 4;

type
    Square* = record
        side*: INT;        (* exported: a user may set it *)
        id: INT;           (* the module's own *)
    end;

var made: INT;             (* only Shape may read or write it *)

(* a square of that side, numbered in the order they are made *)
proc make*(side: INT): Square;
var s: Square;
begin
    INC(made);
    s.side := side;
    s.id := made;
    return s;
end;

proc area*(s: Square): INT;
begin
    return s.side * s.side;
end;

proc count*: INT;
begin
    return made;
end;

end Shape.
Squares.raz
module Squares;
import Out, Shape;

proc main*;
var a, b: Shape.Square;
begin
    a := Shape.make(3);
    b := Shape.make(5);
    Out.int(Shape.area(a) + Shape.area(b)); Out.ln;
    Out.int(Shape.count()); Out.str(" made, "); Out.int(Shape.Sides);
    Out.str(" sides each\n");
end;

end Squares.
34
2 made, 4 sides each

What the compiler refuses

Reaching past an interface is an error, and the error says where.

Peek.raz
module Peek;
import Shape;

proc main*;
var s: Shape.Square;
begin
    s := Shape.make(2);
    s.id := 7;
    Shape.made := 0;
end;

end Peek.
$ raz Peek.raz
Peek.raz:8:7: error: field id of Shape.Square isn't exported
    8 |     s.id := 7;
      |       ^^
./Shape.raz:8:9: note: id is declared here, without a *
    8 |         id: INT;           (* the module's own *)
      |         ^^
Peek.raz:9:11: error: made isn't exported by Shape
    9 |     Shape.made := 0;
      |           ^^^^
./Shape.raz:11:5: note: made is declared here, without a *
   11 | var made: INT;             (* only Shape may read or write it *)
      |     ^^^^
raz: 2 errors

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.

Save a program under its module’s name and run it with raz run Name.raz.