← All examples

Interfaces

A named list of promises about some <T>, announced after the name.

interfaces.zg
# Interfaces (design/interfaces.md): a named list of promises about some <T>, announced after the name.
interface Ordered <T> {
    op < ['a': <T>, 'b': <T>] -> bool;
    op equal_to ['a': <T>, 'b': <T>] -> bool;
}

interface Printable <T> {
    fn show [lent 'x': <T>] -> str.utf8;
}

struct Vec { x: bin64, y: bin64 }

# a struct keeps a promise by having the operations — the language's own operators
# may be defined for a user type (never for the language's types)
op < ['a': Vec, 'b': Vec] -> bool { return ('a'.x times 'a'.x plus 'a'.y times 'a'.y) < ('b'.x times 'b'.x plus 'b'.y times 'b'.y); }
op equal_to ['a': Vec, 'b': Vec] -> bool { return 'a'.x equal_to 'b'.x and 'a'.y equal_to 'b'.y; }
fn show [lent 'v': Vec] -> str.utf8 { return "(" ++ to.stored.str.utf8['v'.x] ++ ", " ++ to.stored.str.utf8['v'.y] ++ ")"; }

Vec satisfies Ordered;        # optional: a claim the compiler checks
Vec satisfies Printable;

# a generic says what its blank accepts where it announces it (owner, 2026-10-02)
fn max <T: Ordered> ['a': <T>, 'b': <T>] -> <T> {
    if 'b' < 'a' { return give 'a'; }     # a <T> may be owned, so generic code writes the verbs;
    return give 'b';                       # for a plain T (int32) the verb does nothing
}

fn describe <T: Printable> [lent 'x': <T>] -> str.utf8 {
    return "value " ++ show[lend 'x'];
}

MAIN {
    let 'u' := Vec[x = 1, y = 2];
    let 'v' := Vec[x = 3, y = 4];
    let 'm' := max[copy 'u', copy 'v'];             # Vec is Ordered: it has < and equal_to 
    std::print.useful[describe[lend 'm'], newln];
    std::print.useful[max[3, 9], " ", max[2.5, 1.5], newln];   # numbers are Ordered by the language
    std::print.useful['u' equal_to 'v', " ", 'u' < 'v', newln];     # the operators work directly too
}
Output
value (3, 4)
9 2.5
false true