x-lang

← Index

x/type/generic

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.

Class Generic

Generic 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:

Returns: ANY — The callable generic

(Generic generic? x)

Test whether a value is a generic function.

Parameters:

Returns: 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:

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:

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:

Returns: BOOL — #t when k1’s type converts from k2

(Generic methods-of g)

The registered (keys . fn) method records, newest first.

Parameters:

Returns: LIST — Method records

def-generic

Define 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)) => 'hit

See also: def-class

on

Add 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)) => 3

See also: def-generic