16. Words (Custom Operators)

Words let you define custom operators from identifiers. Each word has a fixed position — prefix, infix, or suffix — with language-defined precedence.

16.1 Word Types

Type Description Example
operator Overload a specific operator (+, -, *, (), etc.) implement Vec3 as Arithmetic { fn +(self, other: Self): Self { ... } }
token A word with low precedence that does nothing alone. Serves as a named argument for macros and other words. token SELECT;

Operator Words

Operator words overload built-in operators. Use implement with a capability to define the behavior:

implement Vec3 as Arithmetic {
    fn +(self, other: Self): Self { ... }
    fn -(self, other: Self): Self { ... }
    fn *(self, scalar: f32): Self { ... }
}

// Custom word — not overloading a built-in operator
use math.vec.dot as DOT;
use math.vec.cross as CROSS;

// Infix — reads as dot(vec1, vec2)
let d = vec1 DOT vec2;

// Prefix — reads as VALIDATE input
let result = VALIDATE data;

// Suffix — reads as input CHECK
let value = input CHECK;
Position Reads as
Infix a DOT bdot(a, b)
Prefix not xnot(x)
Suffix x!assert(x)

Token Words

Token words have low precedence and do nothing alone. They serve as named arguments for macros and other words — e.g., SQL keywords:

token SELECT;
token FROM;
token WHERE;

// Operator* defines behavior for a token
operator* (SELECT, list) { ... }

Tokens are useful for DSLs where keywords need to be passed as arguments without function call syntax.

16.2 Words vs. Macros

Words let you pass keywords, words, and macros as arguments.


Zith Language Specification — Draft v0.9