1
0
Fork 0
cl-sites/HyperSpec-7-0/HyperSpec/Body/07_fb.htm
2024-04-01 10:24:07 +02:00

54 lines
15 KiB
HTML

<!-- Common Lisp HyperSpec (TM), version 7.0 generated by Kent M. Pitman on Mon, 11-Apr-2005 2:31am EDT -->
<HTML>
<HEAD>
<TITLE>CLHS: Section 7.6.2</TITLE>
<LINK HREF="../Data/clhs.css" REL="stylesheet" TYPE="text/css" />
<META HTTP-EQUIV="Author" CONTENT="Kent M. Pitman">
<META HTTP-EQUIV="Organization" CONTENT="LispWorks Ltd.">
<LINK REL=TOP HREF="../Front/index.htm">
<LINK REL=COPYRIGHT HREF="../Front/Help.htm#Legal">
<LINK REL=DISCLAIMER HREF="../Front/Help.htm#Disclaimer">
<LINK REL=PREV HREF="07_fa.htm">
<LINK REL=UP HREF="07_f.htm">
<LINK REL=NEXT HREF="07_fc.htm">
</HEAD>
<BODY>
<H1><A REV=MADE HREF="http://www.lispworks.com/"><IMG WIDTH=80 HEIGHT=65 ALT="[LISPWORKS]" SRC="../Graphics/LWSmall.gif" ALIGN=Bottom></A><A REL=TOP HREF="../Front/index.htm"><IMG WIDTH=237 HEIGHT=65 ALT="[Common Lisp HyperSpec (TM)]" SRC="../Graphics/CLHS_Sm.gif" ALIGN=Bottom></A> <A REL=PREV HREF="07_fa.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="07_f.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="07_fc.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
<HR>
<H2>
7.6.2 Introduction to Methods</H2> <P>
<A REL=DEFINITION HREF="26_glo_m.htm#method"><I>Methods</I></A> define the class-specific or identity-specific behavior and operations of a <A REL=DEFINITION HREF="26_glo_g.htm#generic_function"><I>generic function</I></A>. <P>
A <A REL=DEFINITION HREF="26_glo_m.htm#method"><I>method</I></A> <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> is associated with <A REL=DEFINITION HREF="26_glo_c.htm#code"><I>code</I></A> that implements the method's behavior, a sequence of <A REL=DEFINITION HREF="26_glo_p.htm#parameter_specializer"><I>parameter specializers</I></A> that specify when the given <A REL=DEFINITION HREF="26_glo_m.htm#method"><I>method</I></A> is applicable, a <A REL=DEFINITION HREF="26_glo_l.htm#lambda_list"><I>lambda list</I></A>, and a sequence of <A REL=DEFINITION HREF="26_glo_q.htm#qualifier"><I>qualifiers</I></A> that are used by the method combination facility to distinguish among <A REL=DEFINITION HREF="26_glo_m.htm#method"><I>methods</I></A>. <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 <A REL=DEFINITION HREF="26_glo_c.htm#code"><I>code</I></A> 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>
<P><DL><P>
<DT>* If a <A REL=DEFINITION HREF="26_glo_g.htm#generic_function"><I>generic function</I></A> of the given name already exists and if a <A REL=DEFINITION HREF="26_glo_m.htm#method"><I>method</I></A> <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> already exists that agrees with the new one on <A REL=DEFINITION HREF="26_glo_p.htm#parameter_specializer"><I>parameter specializers</I></A> and <A REL=DEFINITION HREF="26_glo_q.htm#qualifier"><I>qualifiers</I></A>, the new <A REL=DEFINITION HREF="26_glo_m.htm#method"><I>method</I></A> <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> replaces the old one. For a definition of one method agreeing with another on <A REL=DEFINITION HREF="26_glo_p.htm#parameter_specializer"><I>parameter specializers</I></A> and <A REL=DEFINITION HREF="26_glo_q.htm#qualifier"><I>qualifiers</I></A>, see <A REL=CHILD HREF="07_fc.htm">Section 7.6.3 (Agreement on Parameter Specializers and Qualifiers)</A>. <P><DD>
<DT>* If a <A REL=DEFINITION HREF="26_glo_g.htm#generic_function"><I>generic function</I></A> of the given name already exists and if there is no <A REL=DEFINITION HREF="26_glo_m.htm#method"><I>method</I></A> <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> that agrees with the new one on <A REL=DEFINITION HREF="26_glo_p.htm#parameter_specializer"><I>parameter specializers</I></A> and <A REL=DEFINITION HREF="26_glo_q.htm#qualifier"><I>qualifiers</I></A>, the existing <A REL=DEFINITION HREF="26_glo_g.htm#generic_function"><I>generic function</I></A> <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> is modified to contain the new <A REL=DEFINITION HREF="26_glo_m.htm#method"><I>method</I></A> <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A>. <P><DD>
<DT>* If the given <A REL=DEFINITION HREF="26_glo_n.htm#name"><I>name</I></A> names an <A REL=DEFINITION HREF="26_glo_o.htm#ordinary_function"><I>ordinary function</I></A>, a <A REL=DEFINITION HREF="26_glo_m.htm#macro"><I>macro</I></A>, or a <A REL=DEFINITION HREF="26_glo_s.htm#special_operator"><I>special operator</I></A>, an error is signaled. <P><DD>
<DT>* Otherwise a <A REL=DEFINITION HREF="26_glo_g.htm#generic_function"><I>generic function</I></A> is created with the <A REL=DEFINITION HREF="26_glo_m.htm#method"><I>method</I></A> specified by the <A REL=DEFINITION HREF="26_glo_m.htm#method-defining_form"><I>method-defining form</I></A>. <P><DD>
<P></DL><P>
If the <A REL=DEFINITION HREF="26_glo_l.htm#lambda_list"><I>lambda list</I></A> of a new <A REL=DEFINITION HREF="26_glo_m.htm#method"><I>method</I></A> is not <A REL=DEFINITION HREF="26_glo_c.htm#congruent"><I>congruent</I></A> with the <A REL=DEFINITION HREF="26_glo_l.htm#lambda_list"><I>lambda list</I></A> of the <A REL=DEFINITION HREF="26_glo_g.htm#generic_function"><I>generic function</I></A>, an error is signaled. If a <A REL=DEFINITION HREF="26_glo_m.htm#method-defining_operator"><I>method-defining operator</I></A> that cannot specify <A REL=DEFINITION HREF="26_glo_g.htm#generic_function"><I>generic function</I></A> options creates a new <A REL=DEFINITION HREF="26_glo_g.htm#generic_function"><I>generic function</I></A>, a <A REL=DEFINITION HREF="26_glo_l.htm#lambda_list"><I>lambda list</I></A> for that <A REL=DEFINITION HREF="26_glo_g.htm#generic_function"><I>generic function</I></A> is derived from the <A REL=DEFINITION HREF="26_glo_l.htm#lambda_list"><I>lambda list</I></A> of the <A REL=DEFINITION HREF="26_glo_m.htm#method"><I>method</I></A> in the <A REL=DEFINITION HREF="26_glo_m.htm#method-defining_form"><I>method-defining form</I></A> in such a way as to be <A REL=DEFINITION HREF="26_glo_c.htm#congruent"><I>congruent</I></A> with it. For a discussion of <I>congruence</I>, see <A REL=CHILD HREF="07_fd.htm">Section 7.6.4 (Congruent Lambda-lists for all Methods of a Generic Function)</A>. <P>
Each method has a <A REL=DEFINITION HREF="26_glo_s.htm#specialized_lambda_list"><I>specialized lambda list</I></A>, which determines when that method can be applied. A <A REL=DEFINITION HREF="26_glo_s.htm#specialized_lambda_list"><I>specialized lambda list</I></A> is like an <A REL=DEFINITION HREF="26_glo_o.htm#ordinary_lambda_list"><I>ordinary lambda list</I></A> except that a specialized parameter may occur instead of the name of a required parameter. A specialized parameter is a list <TT>(</TT><I>variable-name</I><TT> </TT><I>parameter-specializer-name</I><TT>)</TT>, where <I>parameter-specializer-name</I> is one of the following: <P>
<P><DL><P>
<DT>a <A REL=DEFINITION HREF="26_glo_s.htm#symbol"><I>symbol</I></A> <P><DD>
denotes a <A REL=DEFINITION HREF="26_glo_p.htm#parameter_specializer"><I>parameter specializer</I></A> which is the <A REL=DEFINITION HREF="26_glo_c.htm#class"><I>class</I></A> named by that <A REL=DEFINITION HREF="26_glo_s.htm#symbol"><I>symbol</I></A>. <P>
<DT>a <A REL=DEFINITION HREF="26_glo_c.htm#class"><I>class</I></A> <P><DD>
denotes a <A REL=DEFINITION HREF="26_glo_p.htm#parameter_specializer"><I>parameter specializer</I></A> which is the <A REL=DEFINITION HREF="26_glo_c.htm#class"><I>class</I></A> itself. <P>
<DT><TT>(eql </TT><I>form</I><TT>)</TT> <P><DD>
denotes a <A REL=DEFINITION HREF="26_glo_p.htm#parameter_specializer"><I>parameter specializer</I></A> which satisfies the <A REL=DEFINITION HREF="26_glo_t.htm#type_specifier"><I>type specifier</I></A> <TT>(eql </TT><I>object</I><TT>)</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. <P></DL><P>
<A REL=DEFINITION HREF="26_glo_p.htm#parameter_specializer_name"><I>Parameter specializer names</I></A> are used in macros intended as the user-level interface (<A REL=DEFINITION HREF="m_defmet.htm#defmethod"><B>defmethod</B></A>), while <A REL=DEFINITION HREF="26_glo_p.htm#parameter_specializer"><I>parameter specializers</I></A> are used in the functional interface. <P>
Only required parameters may be specialized, and there must be a <A REL=DEFINITION HREF="26_glo_p.htm#parameter_specializer"><I>parameter specializer</I></A> for each required parameter. For notational simplicity, if some required parameter in a <A REL=DEFINITION HREF="26_glo_s.htm#specialized_lambda_list"><I>specialized lambda list</I></A> in a method-defining form is simply a variable name, its <A REL=DEFINITION HREF="26_glo_p.htm#parameter_specializer"><I>parameter specializer</I></A> defaults to the <A REL=DEFINITION HREF="26_glo_c.htm#class"><I>class</I></A> <A REL=DEFINITION HREF="t_t.htm#t"><B>t</B></A>. <P>
Given a generic function and a set of arguments, an applicable method 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 <A REL=DEFINITION HREF="26_glo_p.htm#parameter_specializer"><I>parameter specializer</I></A>. <P>
Let &lt;A1, ..., An&gt; be the required arguments to a generic function in order. Let &lt;P1, ..., Pn&gt; be the <A REL=DEFINITION HREF="26_glo_p.htm#parameter_specializer"><I>parameter specializers</I></A> corresponding to the required parameters of the method M in order. The method M is applicable when each Ai is of the <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> specified by the <A REL=DEFINITION HREF="26_glo_t.htm#type_specifier"><I>type specifier</I></A> Pi. Because every valid <A REL=DEFINITION HREF="26_glo_p.htm#parameter_specializer"><I>parameter specializer</I></A> is also a valid <A REL=DEFINITION HREF="26_glo_t.htm#type_specifier"><I>type specifier</I></A>, the <A REL=DEFINITION HREF="26_glo_f.htm#function"><I>function</I></A> <A REL=DEFINITION HREF="f_typep.htm#typep"><B>typep</B></A> can be used during method selection to determine whether an argument satisfies a <A REL=DEFINITION HREF="26_glo_p.htm#parameter_specializer"><I>parameter specializer</I></A>. <P>
A method all of whose <A REL=DEFINITION HREF="26_glo_p.htm#parameter_specializer"><I>parameter specializers</I></A> are the <A REL=DEFINITION HREF="26_glo_c.htm#class"><I>class</I></A> <A REL=DEFINITION HREF="t_t.htm#t"><B>t</B></A> is called a <A REL=DEFINITION HREF="26_glo_d.htm#default_method"><I>default method</I></A>; it is always applicable but may be shadowed by a more specific method. <P>
Methods can have <A REL=DEFINITION HREF="26_glo_q.htm#qualifier"><I>qualifiers</I></A>, which give the method combination procedure a way to distinguish among methods. A method that has one or more <A REL=DEFINITION HREF="26_glo_q.htm#qualifier"><I>qualifiers</I></A> is called a <A REL=DEFINITION HREF="26_glo_q.htm#qualified_method"><I>qualified method</I></A>. A method with no <A REL=DEFINITION HREF="26_glo_q.htm#qualifier"><I>qualifiers</I></A> is called an <A REL=DEFINITION HREF="26_glo_u.htm#unqualified_method"><I>unqualified method</I></A>. A <A REL=DEFINITION HREF="26_glo_q.htm#qualifier"><I>qualifier</I></A> is any <A REL=DEFINITION HREF="26_glo_n.htm#non-list"><I>non-list</I></A>. The <A REL=DEFINITION HREF="26_glo_q.htm#qualifier"><I>qualifiers</I></A> defined by the <A REL=DEFINITION HREF="26_glo_s.htm#standardized"><I>standardized</I></A> method combination types are <A REL=DEFINITION HREF="26_glo_s.htm#symbol"><I>symbols</I></A>. <P>
In this specification, the terms ``<A REL=DEFINITION HREF="26_glo_p.htm#primary_method"><I>primary method</I></A>'' and ``<A REL=DEFINITION HREF="26_glo_a.htm#auxiliary_method"><I>auxiliary method</I></A>'' are used to partition <A REL=DEFINITION HREF="26_glo_m.htm#method"><I>methods</I></A> within a method combination type according to their intended use. In standard method combination, <A REL=DEFINITION HREF="26_glo_p.htm#primary_method"><I>primary methods</I></A> are <A REL=DEFINITION HREF="26_glo_u.htm#unqualified_method"><I>unqualified methods</I></A> and <A REL=DEFINITION HREF="26_glo_a.htm#auxiliary_method"><I>auxiliary methods</I></A> are methods with a single <A REL=DEFINITION HREF="26_glo_q.htm#qualifier"><I>qualifier</I></A> that is one of <TT>:around</TT>, <TT>:before</TT>, or <TT>:after</TT>. <A REL=DEFINITION HREF="26_glo_m.htm#method"><I>Methods</I></A> with these <A REL=DEFINITION HREF="26_glo_q.htm#qualifier"><I>qualifiers</I></A> are called <A REL=DEFINITION HREF="26_glo_a.htm#around_method"><I>around methods</I></A>, <A REL=DEFINITION HREF="26_glo_b.htm#before_method"><I>before methods</I></A>, and <A REL=DEFINITION HREF="26_glo_a.htm#after_method"><I>after methods</I></A>, respectively. When a method combination type is defined using the short form of <A REL=DEFINITION HREF="m_defi_4.htm#define-method-combination"><B>define-method-combination</B></A>, <A REL=DEFINITION HREF="26_glo_p.htm#primary_method"><I>primary methods</I></A> are methods qualified with the name of the type of method combination, and auxiliary methods have the <A REL=DEFINITION HREF="26_glo_q.htm#qualifier"><I>qualifier</I></A> <TT>:around</TT>. Thus the terms ``<A REL=DEFINITION HREF="26_glo_p.htm#primary_method"><I>primary method</I></A>'' and ``<A REL=DEFINITION HREF="26_glo_a.htm#auxiliary_method"><I>auxiliary method</I></A>'' have only a relative definition within a given method combination type. <P>
<P><HR>The following <A REL=META HREF="../Front/X3J13Iss.htm">X3J13 cleanup issue</A>, <I>not part of the specification</I>, applies to this section:<P><UL><LI> <A REL=CHILD HREF="../Issues/iss047.htm">CLASS-OBJECT-SPECIALIZER:AFFIRM</A><P></UL><HR>
<A REL=NAVIGATOR HREF="../Front/StartPts.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Starting Points]" SRC="../Graphics/StartPts.gif" ALIGN=Bottom></A><A REL=TOC HREF="../Front/Contents.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Contents]" SRC="../Graphics/Contents.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Master.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Index]" SRC="../Graphics/Index.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Symbol.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Symbols]" SRC="../Graphics/Symbols.gif" ALIGN=Bottom></A><A REL=GLOSSARY HREF="../Body/26_a.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Glossary]" SRC="../Graphics/Glossary.gif" ALIGN=Bottom></A><A HREF="../Front/X3J13Iss.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Issues]" SRC="../Graphics/Issues.gif" ALIGN=Bottom></A><BR>
<A REL=COPYRIGHT HREF="../Front/Help.htm#Legal"><I>Copyright 1996-2005, LispWorks Ltd. All rights reserved.</I></A><P>
</BODY>
</HTML>