[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Like an ordinary Scheme procedure, a generic procedure takes arguments, performs a series of operations, and perhaps returns useful values. An ordinary procedure has a single body of code that is always executed when the procedure is called. A generic procedure has a set of multiple bodies of code, called methods, from which a subset is selected for execution. The selected bodies of code and the manner of their combination are determined by the classes of one or more of the arguments to the generic procedure.
Ordinary procedures and generic procedures are called with identical procedure-call syntax.
Generic procedures are true procedures that can be passed as arguments,
returned as values, and otherwise used in all the ways an ordinary
procedure may be used. In particular, generic procedures satisfy the
predicate procedure?
.
4.1 Generic Procedure Datatype 4.2 Method Storage 4.3 Effective Method Procedure
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The following definitions are used to construct and inspect generic procedures.
Arity may take one of the following forms. An exact positive
integer specifies that the procedure will accept exactly that number of
arguments. A pair of two exact positive integers specifies inclusive
lower and upper bounds, respectively, on the number of arguments
accepted; the CDR may be #f
indicating no upper bound.
Name is used for debugging: it is a symbol that has no role in the
semantics of the generic procedure. Name may be #f
to
indicate that the generic procedure is anonymous. If name is not
specified, it defaults to #f
.
Examples:
(define foo-bar (make-generic-procedure 2)) (define foo-baz (make-generic-procedure '(1 . 2) 'foo-baz)) (define foo-mum (make-generic-procedure '(1 . #f))) |
lambda
special form. This expands into
(define name (make-generic-procedure arity (quote name))) |
where arity is determined from lambda-list.
Examples (compare to examples of make-generic-procedure
):
(define-generic foo-bar (x y)) (define-generic foo-baz (x #!optional y)) (define-generic foo-mum (x . y)) |
#t
if object is a generic procedure, otherwise
returns #f
. Note that every generic procedure satisfies the
predicate procedure?
.
make-generic-procedure
. The returned arity must not be
modified.
make-generic-procedure
.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Methods are stored in generic procedures. When a generic procedure is
called, it selects a subset of its stored methods (using
method-applicable?
), and arranges to invoke one or more of the
methods as necessary. The following definitions provide the means for
adding methods to and removing them from a generic procedure.
add-method
on
each method in methods.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
When a generic procedure is called, it arranges to invoke a subset of
its methods. This is done by combining the selected methods into
an effective method procedure, or EMP, then tail-recursively
invoking the EMP. compute-effective-method-procedure
is the
procedure that is called to select the applicable methods and combine
them into an EMP.
method-applicable?
on each method and on classes. Combines
the resulting methods together into an effective method procedure, and
returns that EMP.
compute-effective-method-procedure
, except
that it returns the result as a method whose specializers are
classes.
compute-method
is equivalent to
(make-method classes (compute-effective-method-procedure generic-procedure classes)) |
[ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |