emacs.d/clones/www.cs.cmu.edu/Groups/AI/html/cltl/clm/node279.html
2022-08-26 19:11:35 +02:00

177 lines
11 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
<!Converted with LaTeX2HTML 0.6.5 (Tue Nov 15 1994) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
<HEAD>
<TITLE>28.1.6.2. Introduction to Methods</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Introduction to Methods">
<meta name="keywords" value="clm">
<meta name="resource-type" value="document">
<meta name="distribution" value="global">
<P>
<b>Common Lisp the Language, 2nd Edition</b>
<BR> <HR><A NAME=tex2html5147 HREF="node280.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html5145 HREF="node277.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html5139 HREF="node278.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html5149 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html5150 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html5148 HREF="node280.html"> Agreement on Parameter </A>
<B>Up:</B> <A NAME=tex2html5146 HREF="node277.html"> Generic Functions and </A>
<B> Previous:</B> <A NAME=tex2html5140 HREF="node278.html"> Introduction to Generic </A>
<HR> <P>
<H3><A NAME=SECTION003216200000000000000>28.1.6.2. Introduction to Methods</A></H3>
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
<A NAME=IntroductiontoMethodsSECTION>A</A>
method object contains a method function, a sequence of <i>parameter specializers</i> that specify when the given method is
applicable, a lambda-list, and a sequence of <i>qualifiers</i> that
are used by the method combination facility to distinguish among
methods.
<P>
A method object is not a function and cannot be invoked as a function.
Various mechanisms in the Object System take a method object and invoke its method
function, as is the case when a generic function is invoked. When this
occurs it is said that the method is invoked or called.
<P>
A method-defining form contains the code that is to be run when the
arguments to the generic function cause the method that it defines to
be invoked. When a method-defining form is evaluated, a method object
is created and one of four actions is taken:
<P>
<UL><LI> If a generic function of the given name already exists
and if a method object already exists that agrees with the new one on
parameter specializers and qualifiers, the new method object replaces
the old one. For a definition of one method agreeing with another on
parameter specializers and qualifiers, see
section <A HREF="node280.html#AgreementonParameterSpecializersandQualifiersSECTION">28.1.6.3</A>.
<P>
<LI> If a generic function of the given name already exists
and if there is no method object that agrees with the new one on
parameter specializers and qualifiers, the existing generic function
object is modified to contain the new method object.
<P>
<LI> If the given name names a non-generic function, a macro,
or a special form, an error is signaled.
<P>
<LI> Otherwise a generic function is created with the methods
specified by the method-defining form.
<P>
</UL>
<P>
If the lambda-list of a new method is not congruent with the lambda-list
of the generic function, an error is signaled. If a
method-defining form that cannot specify generic function options
creates a new generic function, a lambda-list for that generic
function is derived from the lambda-lists of the methods in the
method-defining form in such a way as to be congruent with them. For
a discussion of <i>congruence</i>, see
section <A HREF="node281.html#CongruentLambdaListsforAllMethodsofaGenericFunctionSECTION">28.1.6.4</A>.
<P>
Each method has a <i>specialized lambda-list</i>, which determines
when that method can be applied. A specialized lambda-list is like
an ordinary lambda-list except that a <i>specialized parameter</i>
may occur instead of the name of a required parameter. A specialized parameter
is a list <tt>(<i>variable-name parameter-specializer-name</i>)</tt>,
where <i>parameter-specializer-name</i> is either
a name that names a class or a list <tt>(<tt>eql</tt> <i>form</i>)</tt>.
A parameter specializer name denotes a parameter specializer as follows:
<P>
<UL><LI> A name that names a class denotes that class.
<P>
<LI> The list <tt>(<tt>eql</tt> <i>form</i>)</tt> denotes the type specifier
<tt>(<tt>eql</tt> <i>object</i>)</tt>, where <i>object</i> is the result of
evaluating <i>form</i>. The form <i>form</i> is evaluated in the
lexical environment in which the method-defining form is
evaluated. Note that <i>form</i> is evaluated only once, at the time
the method is defined, not each time the generic function is called.
</UL>
<P>
Parameter specializer names are used in macros intended as the
user-level interface (<tt>defmethod</tt>), while parameter specializers
are used in the functional interface.
<P>
[It is very important to understand clearly the distinction made
in the preceding paragraph. A parameter specializer name
has the form of a type specifier but is semantically quite different
from a type specifier: a parameter specializer name of the form
<tt>(<tt>eql</tt> <i>form</i>)</tt> is not a type specifier, for it contains
a <i>form</i> to be evaluated. Type specifiers
never contain forms to be evaluated. All parameter specializers
(as opposed to parameter specializer names) are valid type specifiers,
but not all type specifiers are valid parameter specializers. Macros such as <tt>defmethod</tt>
take parameter specializer names and treat them as specifications for
constructing certain type specifiers (parameter specializers) that may then be used
with such functions as <tt>find-method</tt>.-GLS]
<P>
Only required parameters may be specialized, and there must be a
parameter specializer for each required parameter. For notational
simplicity, if some required parameter in a specialized lambda-list in
a method-defining form is simply a variable name, its parameter
specializer defaults to the class named <tt>t</tt>.
<P>
Given a generic function and a set of arguments, an <i>applicable
method</i> is a method for that generic function whose parameter
specializers are satisfied by their corresponding arguments. The
following definition specifies what it means for a method to be
applicable and for an argument to satisfy a parameter specializer.
<P>
Let <IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap43517.gif"> be the required
arguments to a generic function in order. Let <IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap43519.gif"> be the parameter specializers corresponding to
the required parameters of the method <i>M</i> in order. The method <i>M</i> is
<i>applicable</i> when each <IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap43543.gif">
<i>satisfies</i> <IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap43523.gif">.
If <IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap43523.gif"> is a class,
and if <IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap43543.gif"> is an instance of a class
<i>C</i>, then it is said that <IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap43543.gif"> <i>satisfies</i>
<IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap43523.gif"> when <IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap43533.gif"> or when <i>C</i> is a subclass of <IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap43523.gif">. If
<IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap43523.gif"> is of the form
<tt>(<tt>eql</tt> <i>object</i>)</tt>, then it is said that
<IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap43543.gif"> satisfies <IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap43523.gif">
when the function <tt>eql</tt> applied to
<IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap43543.gif"> and <i>object</i> is true.
<P>
Because a parameter specializer is a type specifier, the function
<tt>typep</tt> can be used during method selection to determine whether an
argument satisfies a parameter specializer. In general a
parameter specializer cannot be a type specifier list, such as
<tt>(<tt>vector single-float</tt>)</tt>. The only parameter specializer that can
be a list is <tt>(<tt>eql</tt> <i>object</i>)</tt>. This requires that
Common Lisp define the type specifier <tt>eql</tt>
as if the following were evaluated:
<P>
<P><pre>
(deftype eql (<i>object</i>) `(member ,<i>object</i>))
</pre><P>
[See section <A HREF="node47.html#PREDICATINGTYPESPECIFIERSSECTION">4.3</A>.-GLS]
<P>
A method all of whose parameter specializers are the class named
<tt>t</tt> is called a <i>default method</i>; it is always applicable but
may be shadowed by a more specific method.
<P>
Methods can have <i>qualifiers</i>, which give the method combination
procedure a way to distinguish among methods. A method that has one
or more qualifiers is called a <i>qualified</i> method.
A method with no qualifiers is called an <i>unqualified method</i>.
A qualifier is any object other than a list, that is,
any non-<tt>nil</tt> atom. The qualifiers defined by standard method combination
and by the built-in method combination types are symbols.
<P>
In this specification, the terms <i>primary method</i> and <i>auxiliary method</i> are used to partition methods within a method
combination type according to their intended use. In standard method
combination, primary methods are unqualified methods, and auxiliary
methods are methods with a single qualifier that is one of
<tt>:around</tt>, <tt>:before</tt>, or <tt>:after</tt>. When a method combination
type is defined using the short form of
<tt>define-method-combination</tt>, primary methods are methods qualified with
the name of the type of method combination, and auxiliary methods have
the qualifier <tt>:around</tt>. Thus the terms <i>primary method</i>
and <i>auxiliary method</i> have only a relative definition within a
given method combination type.
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
<BR> <HR><A NAME=tex2html5147 HREF="node280.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html5145 HREF="node277.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html5139 HREF="node278.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html5149 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html5150 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html5148 HREF="node280.html"> Agreement on Parameter </A>
<B>Up:</B> <A NAME=tex2html5146 HREF="node277.html"> Generic Functions and </A>
<B> Previous:</B> <A NAME=tex2html5140 HREF="node278.html"> Introduction to Generic </A>
<HR> <P>
<HR>
<P><ADDRESS>
AI.Repository@cs.cmu.edu
</ADDRESS>
</BODY>