← All examples

A generic operator

Written once for any type that keeps its interface's promises, like a generic fn.

generic_op.zg
# A generic operator (owner, 2026-09-29): written once for any type that keeps its interface's
# promises, like a generic fn. An operator may also be written for several types by name; two
# meanings for the same type are an error, a generic one covering a specific one's type included.
interface Number <T> {
    op minus ['a': <T>, 'b': <T>] -> <T>;
    op < ['a': <T>, 'b': <T>] -> bool;
}
op ~= <T: Number> ['a': <T>, 'b': <T>] -> bool {
    if 'a' < 'b' { return 'b' minus 'a' < 10; }
    return 'a' minus 'b' < 10;
}
struct A { x: int32 }
struct B { y: int32 }
op <+> ['a': A, 'b': A] -> int32 { return 'a'.x plus 'b'.x; }
op <+> ['a': B, 'b': B] -> int32 { return 'a'.y times 'b'.y; }
MAIN {
    let 'm': int32 = 100;
    std::print.useful[95 ~= 'm', " ", 50 ~= 'm', newln];
    let 'p' := A[x = 3];
    let 'q' := B[y = 3];
    std::print.useful['p' <+> 'p', " ", 'q' <+> 'q', newln];
}
Output
true false
6 9