| 1 | title: Generic modules |
| 2 | part: Go |
| 3 | summary: A module that takes types and procedures; the library's List, Map and Sort. |
| 4 | --- |
| 5 | A module may take types, and the procedures it needs on them, as parameters. `Biggest.raz`: |
| 6 | |
| 7 | ```raz Biggest |
| 8 | ``` |
| 9 | |
| 10 | and a program that uses it: |
| 11 | |
| 12 | ```raz Generics run |
| 13 | ``` |
| 14 | |
| 15 | 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`. |
| 16 | |
| 17 | ## Writing your own |
| 18 | |
| 19 | 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. |
| 20 | |
| 21 | ```raz Stk |
| 22 | module Stk(T); |
| 23 | import Mem; |
| 24 | import SYSTEM; |
| 25 | |
| 26 | type |
| 27 | Stack* = record |
| 28 | items: ^T; (* an array of cap Ts *) |
| 29 | cap*, len*: INT; |
| 30 | a: ^Mem.Allocator; |
| 31 | end; |
| 32 | |
| 33 | proc init*(s: ^Stack; a: ^Mem.Allocator); |
| 34 | begin |
| 35 | s^.a := a; |
| 36 | s^.items := nil; |
| 37 | s^.cap := 0; |
| 38 | s^.len := 0; |
| 39 | end; |
| 40 | |
| 41 | (* v on top; false if the allocator has no room *) |
| 42 | proc push*(s: ^Stack; v: T): BOOL; |
| 43 | var bigger: ^T; i, cap: INT; src, dst: ^T; |
| 44 | begin |
| 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; |
| 62 | end; |
| 63 | |
| 64 | (* the top value, taken off; false if empty *) |
| 65 | proc pop*(s: ^Stack): (T, BOOL); |
| 66 | var z: T; p: ^T; |
| 67 | begin |
| 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; |
| 72 | end; |
| 73 | |
| 74 | end Stk. |
| 75 | ``` |
| 76 | |
| 77 | ```raz Uses run |
| 78 | module Uses; |
| 79 | import Out, Heap, Nums := Stk(INT), Pairs := Stk(Pair); |
| 80 | import SYSTEM; |
| 81 | |
| 82 | type Pair = record a, b: INT; end; |
| 83 | |
| 84 | proc main*; |
| 85 | var n: Nums.Stack; p: Pairs.Stack; i, v: INT; ok: BOOL; pr: Pair; |
| 86 | begin |
| 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; |
| 97 | end; |
| 98 | |
| 99 | end Uses. |
| 100 | ``` |
| 101 | |
| 102 | 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. |
| 103 | |
| 104 | ## The library's generics |
| 105 | |
| 106 | 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. |