raz / site / content / tour / 13-generics.txt

106 lines · 3.2 KiB · raw · history · last change a5145f64

1title: Generic modules
2part: Go
3summary: A module that takes types and procedures; the library's List, Map and Sort.
4---
5A module may take types, and the procedures it needs on them, as parameters. `Biggest.raz`:
6
7```raz Biggest
8```
9
10and a program that uses it:
11
12```raz Generics run
13```
14
15Each 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`.
16
17## Writing your own
18
19A generic module is an ordinary module with parameters in its header. Here is a stack of anything, which takes the allocator it grows from.
20
21```raz Stk
22module Stk(T);
23import Mem;
24import SYSTEM;
25
26type
27 Stack* = record
28 items: ^T; (* an array of cap Ts *)
29 cap*, len*: INT;
30 a: ^Mem.Allocator;
31 end;
32
33proc init*(s: ^Stack; a: ^Mem.Allocator);
34begin
35 s^.a := a;
36 s^.items := nil;
37 s^.cap := 0;
38 s^.len := 0;
39end;
40
41(* v on top; false if the allocator has no room *)
42proc push*(s: ^Stack; v: T): BOOL;
43var bigger: ^T; i, cap: INT; src, dst: ^T;
44begin
45 if s^.len = s^.cap then
46 if s^.cap = 0 then cap := 4; else cap := s^.cap * 2; end;
47 bigger := ^T(Mem.alloc(s^.a, cap * SIZE(T), 8));
48 if bigger = nil then return false; end;
49 for i := 0 to s^.len - 1 do
50 src := ^T(INT(s^.items) + i * SIZE(T));
51 dst := ^T(INT(bigger) + i * SIZE(T));
52 dst^ := src^;
53 end;
54 if s^.items # nil then Mem.free(s^.a, ^U8(s^.items)); end;
55 s^.items := bigger;
56 s^.cap := cap;
57 end;
58 dst := ^T(INT(s^.items) + s^.len * SIZE(T));
59 dst^ := v;
60 INC(s^.len);
61 return true;
62end;
63
64(* the top value, taken off; false if empty *)
65proc pop*(s: ^Stack): (T, BOOL);
66var z: T; p: ^T;
67begin
68 if s^.len = 0 then return z, false; end;
69 DEC(s^.len);
70 p := ^T(INT(s^.items) + s^.len * SIZE(T));
71 return p^, true;
72end;
73
74end Stk.
75```
76
77```raz Uses run
78module Uses;
79import Out, Heap, Nums := Stk(INT), Pairs := Stk(Pair);
80import SYSTEM;
81
82type Pair = record a, b: INT; end;
83
84proc main*;
85var n: Nums.Stack; p: Pairs.Stack; i, v: INT; ok: BOOL; pr: Pair;
86begin
87 Nums.init(SYSTEM.ADR(n), Heap.heap);
88 Pairs.init(SYSTEM.ADR(p), Heap.heap);
89 for i := 1 to 10 do _ := Nums.push(SYSTEM.ADR(n), i * i); end;
90 _ := Pairs.push(SYSTEM.ADR(p), {a: 3, b: 4});
91 v, ok := Nums.pop(SYSTEM.ADR(n));
92 Out.int(v); Out.str(" of "); Out.int(n.len + 1); Out.ln;
93 pr, ok := Pairs.pop(SYSTEM.ADR(p));
94 Out.int(pr.a * pr.b); Out.ln;
95 _, ok := Pairs.pop(SYSTEM.ADR(p));
96 if not ok then Out.str("empty\n"); end;
97end;
98
99end Uses.
100```
101
102Two 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.
103
104## The library's generics
105
106The 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.