Generic functions over the class and type systems.
Message passing stays the hot path; generics are the multi-argument cold path.
The ambiguity rule: pointwise specificity, then the cvt from-lattice, then a
teaching error naming both candidates. No scalar rank, by ruling.
GenericGeneric functions: open, multi-argument, type-directed dispatch. def-generic defines one; (on g (SIG…) body) adds a method; the generic itself is a callable value.
(Generic make name)Create a generic function value with no methods.
Parameters:
SYMBOL — The generic’s name (for errors and printing)Returns: ANY — The callable generic
(Generic generic? x)Test whether a value is a generic function.
Parameters:
ANY — Value to testReturns: BOOL — #t for a generic
(Generic add! g keys f)Register a method on a generic (the computed-signature door; (on …) is the sugar). The newest registration wins ties with an identical signature.
Parameters:
ANY — The generickeys : LIST — Signature keys: class |
type handle | #t wildcard, one per argument |
CALLABLE — The method body, (fn (_ args…) …)Returns: ANY — nil
(Generic miss! g f)Install the no-applicable-method handler (the numeric tower’s lattice promotion rides here). nil restores the default error.
Parameters:
ANY — The genericCALLABLE — Handler: (fn (_ g args) …)Returns: ANY — nil
(Generic absorbs? k1 k2)Whether k1 absorbs k2 through the conversion lattice: k1 is a type handle whose type declares a conversion from k2, the relation the C operator arbitration reads. A class or wildcard absorbs nothing. The numeric tower’s promotion asks this at its miss handler.
Parameters:
ANY — A signature key: a type handle, a class, or the #t wildcardANY — The key it may absorbReturns: BOOL — #t when k1’s type converts from k2
(Generic methods-of g)The registered (keys . fn) method records, newest first.
Parameters:
ANY — The genericReturns: LIST — Method records
def-genericDefine a generic function.
(def-generic NAME [“doc”]) – define NAME as a fresh generic. Methods
arrive via (on NAME (SIG…) body…) or (Generic add! NAME keys fn).
Examples:
(do (def-generic f) (on f (x) 'hit) (f 1)) => 'hitSee also: def-class
onAdd a method to a generic function for a signature of argument types.
(on G ((a Class) b (c Type-handle)) body…) – add a method to generic G.
An annotated (name KEY) position matches KEY (a class, subclasses included,
or a type handle, exactly); a bare name matches anything. The body is a
closure over the definition site, parameters bound by name.
Examples:
(do (def-generic f) (on f (x y) (+ x y)) (f 1 2)) => 3See also: def-generic