Go · lesson 13 of 15

Generic modules

A module may take types, and the procedures it needs on them, as parameters. Biggest.raz:

Biggest.raz
module Biggest(T; less: proc(a, b: T): BOOL);

(* the biggest of s, which mustn't be empty *)
proc max*(s: []T): T;
var i: INT; m: T;
begin
    m := s[0];
    for i := 1 to LEN(s) - 1 do
        if less(m, s[i]) then m := s[i]; end;
    end;
    return m;
end;

end Biggest.

and a program that uses it:

Generics.raz
module Generics;
import Out, Ints := Biggest(INT, before),
    Words := Biggest([]const U8, shorter), Order := Sort(INT, before);

proc before(a, b: INT): BOOL;
begin
    return a < b;
end;

proc shorter(a, b: []const U8): BOOL;
begin
    return LEN(a) < LEN(b);
end;

proc main*;
var a: [5]INT; i: INT;
begin
    a := [3, 1, 4, 1, 5];
    Out.int(Ints.max(a)); Out.ln;
    Out.str(Words.max(["a", "tree", "is"])); Out.ln;
    Order.sort(a);
    for i := 0 to 4 do Out.int(a[i]); Out.char(' '); end;
    Out.ln;
end;

end Generics.
5
tree
1 1 3 4 5 

Each import with its own arguments makes an instance: the module compiled again, with T standing for the type given, and less calling the procedure given. The arguments may name what the importing module declares later, as before is here. There is no run-time cost for being generic, and no boxing: Ints.max is exactly the code you would write by hand for INT.

Writing your own

A generic module is an ordinary module with parameters in its header. Here is a stack of anything, which takes the allocator it grows from.

Stk.raz
module Stk(T);
import Mem;
import SYSTEM;

type
    Stack* = record
        items: ^T;         (* an array of cap Ts *)
        cap*, len*: INT;
        a: ^Mem.Allocator;
    end;

proc init*(s: ^Stack; a: ^Mem.Allocator);
begin
    s^.a := a;
    s^.items := nil;
    s^.cap := 0;
    s^.len := 0;
end;

(* v on top; false if the allocator has no room *)
proc push*(s: ^Stack; v: T): BOOL;
var bigger: ^T; i, cap: INT; src, dst: ^T;
begin
    if s^.len = s^.cap then
        if s^.cap = 0 then cap := 4; else cap := s^.cap * 2; end;
        bigger := ^T(Mem.alloc(s^.a, cap * SIZE(T), 8));
        if bigger = nil then return false; end;
        for i := 0 to s^.len - 1 do
            src := ^T(INT(s^.items) + i * SIZE(T));
            dst := ^T(INT(bigger) + i * SIZE(T));
            dst^ := src^;
        end;
        if s^.items # nil then Mem.free(s^.a, ^U8(s^.items)); end;
        s^.items := bigger;
        s^.cap := cap;
    end;
    dst := ^T(INT(s^.items) + s^.len * SIZE(T));
    dst^ := v;
    INC(s^.len);
    return true;
end;

(* the top value, taken off; false if empty *)
proc pop*(s: ^Stack): (T, BOOL);
var z: T; p: ^T;
begin
    if s^.len = 0 then return z, false; end;
    DEC(s^.len);
    p := ^T(INT(s^.items) + s^.len * SIZE(T));
    return p^, true;
end;

end Stk.
Uses.raz
module Uses;
import Out, Heap, Nums := Stk(INT), Pairs := Stk(Pair);
import SYSTEM;

type Pair = record a, b: INT; end;

proc main*;
var n: Nums.Stack; p: Pairs.Stack; i, v: INT; ok: BOOL; pr: Pair;
begin
    Nums.init(SYSTEM.ADR(n), Heap.heap);
    Pairs.init(SYSTEM.ADR(p), Heap.heap);
    for i := 1 to 10 do _ := Nums.push(SYSTEM.ADR(n), i * i); end;
    _ := Pairs.push(SYSTEM.ADR(p), {a: 3, b: 4});
    v, ok := Nums.pop(SYSTEM.ADR(n));
    Out.int(v); Out.str(" of "); Out.int(n.len + 1); Out.ln;
    pr, ok := Pairs.pop(SYSTEM.ADR(p));
    Out.int(pr.a * pr.b); Out.ln;
    _, ok := Pairs.pop(SYSTEM.ADR(p));
    if not ok then Out.str("empty\n"); end;
end;

end Uses.
100 of 10
12
empty

Two instances (Nums and Pairs) are two separate modules, compiled with their own T. Its pointer arithmetic is the one place where the program does what C does and leaves the checks behind; a library would keep that inside, as this does.

The library's generics

The library's List, Map and Sort are generic modules. A Map takes its key hash and equality as procedure arguments: import Count := Map([]const U8, INT, Str.hash, Str.eq);. The advanced tour uses each of them in a real program.

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