raz / STANDARDS.md

70 lines · 3.3 KiB · raw · history · last change 122c3f10

raz: standards #

How raz is written here: in src/, lib/, tools/ and examples/, and in the programs and libraries that use them (rpg, rsqlite, rmacs, ravite, ...). The report says what a program may do; this says what we do. New code keeps to it; old code comes to it when it is next changed. The library came to it in report 2026-10.33.

Parameters: a value, const, var, or a pointer #

A procedure takes a variable it is given in one of four ways, by what it does with it:

The procedureThe parameterExample
reads it, a single value or a slicea valueSys.write(fd: INT; s: []U8)
reads it, a record or an arrayconstMap.get(const m: Map; k: K)
changes it, and lets go of it when it returnsvarBuf.add(var b: Buf; c: U8)
keeps it past the call, in a record, a module variable or another threada pointer, ^TThread.spawn(p, arg: ^U8)

A const parameter is passed in place, as a var one is, so nothing is copied; and since it can't be changed, var in a heading always means the procedure changes what it is given. A record of only four words passed as a value, copied at each call, made a loop of Map.get and Vec.all half again as slow as const did (2026-10.33).

So ADR at a call says the callee keeps what it is given. A reader who sees Pg.start(q, ADR(c), sql) knows that q is changed and c is kept, and that c must outlive q.

proc start*(var q: Query; c: ^Conn; sql: []U8);   (* q changed; c kept in q *)
proc run*(q: Query; var r: Result): ERROR;         (* q read; r changed *)
proc rows*(r: Result): INT;                        (* r read: one word *)
proc clear*(var r: Result);                        (* r changed *)

A pointer is right also where var can't serve:

Anything else that takes ^T and neither keeps it nor needs one of the above is a mistake, and its callers' ADRs are noise.

A module tests itself #

A library or support module has its own proc main* as its test, and raztest runs it. It states what it expects with Check (ok, str, int), which reports every failure and ends the run with status 1, and tests a property of its code, one that must hold for any input, with Prop, drawing bytes or numbers from its Source. A test that passes says nothing. The core modules, and those the compiler imports, keep their own checks, since they can't import a standard-library module.

A match arm names every field #

An arm of a match on a variant names all of a case's fields, in order, or none. A field the arm doesn't read is named with _ before its field's name, as in | real(r, _places):, so a reader sees it was left unread on purpose and -Werror doesn't warn of it.

source
1# raz: standards
2
3How raz is written here: in `src/`, `lib/`, `tools/` and `examples/`,
4and in the programs and libraries that use them (rpg, rsqlite, rmacs,
5ravite, ...). The report says what a program may do; this says what we
6do. New code keeps to it; old code comes to it when it is next changed.
7The library came to it in report 2026-10.33.
8
9## Parameters: a value, `const`, `var`, or a pointer
10
11A procedure takes a variable it is given in one of four ways, by what
12it does with it:
13
14| The procedure | The parameter | Example |
15|---|---|---|
16| reads it, a single value or a slice | a value | `Sys.write(fd: INT; s: []U8)` |
17| reads it, a record or an array | `const` | `Map.get(const m: Map; k: K)` |
18| changes it, and lets go of it when it returns | `var` | `Buf.add(var b: Buf; c: U8)` |
19| keeps it past the call, in a record, a module variable or another thread | a pointer, `^T` | `Thread.spawn(p, arg: ^U8)` |
20
21A `const` parameter is passed in place, as a `var` one is, so nothing
22is copied; and since it can't be changed, `var` in a heading always
23means the procedure changes what it is given. A record of only four
24words passed as a value, copied at each call, made a loop of `Map.get`
25and `Vec.all` half again as slow as `const` did (2026-10.33).
26
27So `ADR` at a call says the callee keeps what it is given. A reader
28who sees `Pg.start(q, ADR(c), sql)` knows that `q` is changed and `c`
29is kept, and that `c` must outlive `q`.
30
31```
32proc start*(var q: Query; c: ^Conn; sql: []U8); (* q changed; c kept in q *)
33proc run*(q: Query; var r: Result): ERROR; (* q read; r changed *)
34proc rows*(r: Result): INT; (* r read: one word *)
35proc clear*(var r: Result); (* r changed *)
36```
37
38A pointer is right also where `var` can't serve:
39
40- **An allocator**, `^Mem.Allocator`: `NEW` takes one, and an
41 allocator such as `Arena` is reached through its first field,
42 `^Arena(a)`, which a `var` of exactly one type can't do.
43- **What may be absent**: a pointer may be `nil`; a `var` can't.
44- **What the program already holds by pointer**: what `NEW` or the
45 library hands out, such as `Thread.spawn`'s `^Thread`, is passed on
46 as it is, not as `p^`.
47- **C**, and memory that is bytes, not a variable: `extern`
48 procedures, `Mem.pages`, `Sys.mmap`, a buffer's address.
49- **A procedure value's argument** that holds what the procedure will
50 need, as `Thread.spawn`'s `arg: ^U8`, which is kept.
51
52Anything else that takes `^T` and neither keeps it nor needs one of the
53above is a mistake, and its callers' `ADR`s are noise.
54
55## A module tests itself
56
57A library or support module has its own `proc main*` as its test, and
58`raztest` runs it. It states what it expects with `Check` (`ok`, `str`,
59`int`), which reports every failure and ends the run with status 1, and
60tests a property of its code, one that must hold for any input, with
61`Prop`, drawing bytes or numbers from its `Source`. A test that passes
62says nothing. The core modules, and those the compiler imports, keep
63their own checks, since they can't import a standard-library module.
64
65## A match arm names every field
66
67An arm of a `match` on a variant names all of a case's fields, in
68order, or none. A field the arm doesn't read is named with `_` before
69its field's name, as in `| real(r, _places):`, so a reader sees it was
70left unread on purpose and `-Werror` doesn't warn of it.