1
0
Fork 0
cl-sites/HyperSpec-7-0/HyperSpec/Body/07_fa.htm

48 lines
11 KiB
HTML
Raw Permalink Normal View History

2024-04-01 10:24:07 +02:00
<!-- 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.1</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_f.htm">
<LINK REL=UP HREF="07_f.htm">
<LINK REL=NEXT HREF="07_fb.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_f.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_fb.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
<HR>
<H2>
7.6.1 Introduction to Generic Functions</H2> <P>
A <A REL=DEFINITION HREF="26_glo_g.htm#generic_function"><I>generic function</I></A> is a function whose behavior depends on the <A REL=DEFINITION HREF="26_glo_c.htm#class"><I>classes</I></A> or identities of the <A REL=DEFINITION HREF="26_glo_a.htm#argument"><I>arguments</I></A> supplied to it. A <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 associated with a set of <A REL=DEFINITION HREF="26_glo_m.htm#method"><I>methods</I></A>, a <A REL=DEFINITION HREF="26_glo_l.htm#lambda_list"><I>lambda list</I></A>, a <A REL=DEFINITION HREF="26_glo_m.htm#method_combination"><I>method combination</I></A>[2], and other information. <P>
Like an <A REL=DEFINITION HREF="26_glo_o.htm#ordinary_function"><I>ordinary function</I></A>, a <A REL=DEFINITION HREF="26_glo_g.htm#generic_function"><I>generic function</I></A> takes <A REL=DEFINITION HREF="26_glo_a.htm#argument"><I>arguments</I></A>, performs a series of operations, and perhaps returns useful <A REL=DEFINITION HREF="26_glo_v.htm#value"><I>values</I></A>. An <A REL=DEFINITION HREF="26_glo_o.htm#ordinary_function"><I>ordinary function</I></A> has a single body of <A REL=DEFINITION HREF="26_glo_c.htm#code"><I>code</I></A> that is always <I>executed</I> when the <A REL=DEFINITION HREF="26_glo_f.htm#function"><I>function</I></A> is called. A <A REL=DEFINITION HREF="26_glo_g.htm#generic_function"><I>generic function</I></A> has a set of bodies of <A REL=DEFINITION HREF="26_glo_c.htm#code"><I>code</I></A> of which a subset is selected for <I>execution</I>. The selected bodies of <A REL=DEFINITION HREF="26_glo_c.htm#code"><I>code</I></A> and the manner of their combination are determined by the <A REL=DEFINITION HREF="26_glo_c.htm#class"><I>classes</I></A> or identities of one or more of the <A REL=DEFINITION HREF="26_glo_a.htm#argument"><I>arguments</I></A> to the <A REL=DEFINITION HREF="26_glo_g.htm#generic_function"><I>generic function</I></A> and by its <A REL=DEFINITION HREF="26_glo_m.htm#method_combination"><I>method combination</I></A>. <P>
<A REL=DEFINITION HREF="26_glo_o.htm#ordinary_function"><I>Ordinary functions</I></A> and <A REL=DEFINITION HREF="26_glo_g.htm#generic_function"><I>generic functions</I></A> are called with identical syntax. <P>
<A REL=DEFINITION HREF="26_glo_g.htm#generic_function"><I>Generic functions</I></A> are true <A REL=DEFINITION HREF="26_glo_f.htm#function"><I>functions</I></A> that can be passed as <A REL=DEFINITION HREF="26_glo_a.htm#argument"><I>arguments</I></A> and used as the first <A REL=DEFINITION HREF="26_glo_a.htm#argument"><I>argument</I></A> to <A REL=DEFINITION HREF="f_funcal.htm#funcall"><B>funcall</B></A> and <A REL=DEFINITION HREF="f_apply.htm#apply"><B>apply</B></A>. <P>
A <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>binding</I></A> of a <A REL=DEFINITION HREF="26_glo_f.htm#function_name"><I>function name</I></A> to a <A REL=DEFINITION HREF="26_glo_g.htm#generic_function"><I>generic function</I></A> can be <I>established</I> in one of several ways. It can be <I>established</I> in the <A REL=DEFINITION HREF="26_glo_g.htm#global_environment"><I>global environment</I></A> by <A REL=DEFINITION HREF="f_ensure.htm#ensure-generic-function"><B>ensure-generic-function</B></A>, <A REL=DEFINITION HREF="m_defmet.htm#defmethod"><B>defmethod</B></A> (implicitly, due to <A REL=DEFINITION HREF="f_ensure.htm#ensure-generic-function"><B>ensure-generic-function</B></A>) or <A REL=DEFINITION HREF="m_defgen.htm#defgeneric"><B>defgeneric</B></A> (also implicitly, due to <A REL=DEFINITION HREF="f_ensure.htm#ensure-generic-function"><B>ensure-generic-function</B></A>). No <A REL=DEFINITION HREF="26_glo_s.htm#standardized"><I>standardized</I></A> mechanism is provided for <I>establishing</I> a <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>binding</I></A> of a <A REL=DEFINITION HREF="26_glo_f.htm#function_name"><I>function name</I></A> to a <A REL=DEFINITION HREF="26_glo_g.htm#generic_function"><I>generic function</I></A> in the <A REL=DEFINITION HREF="26_glo_l.htm#lexical_environment"><I>lexical environment</I></A>. <P>
<P>
<P>
When a <A REL=DEFINITION HREF="m_defgen.htm#defgeneric"><B>defgeneric</B></A> form is evaluated, one of three actions is taken (due to <A REL=DEFINITION HREF="f_ensure.htm#ensure-generic-function"><B>ensure-generic-function</B></A>): <P>
<P><DL><P>
<DT>* If a generic function of the given name already exists, the existing generic function object is modified. Methods specified by the current <A REL=DEFINITION HREF="m_defgen.htm#defgeneric"><B>defgeneric</B></A> form are added, and any methods in the existing generic function that were defined by a previous <A REL=DEFINITION HREF="m_defgen.htm#defgeneric"><B>defgeneric</B></A> form are removed. Methods added by the current <A REL=DEFINITION HREF="m_defgen.htm#defgeneric"><B>defgeneric</B></A> form might replace methods defined by <A REL=DEFINITION HREF="m_defmet.htm#defmethod"><B>defmethod</B></A>, <A REL=DEFINITION HREF="m_defcla.htm#defclass"><B>defclass</B></A>, <A REL=DEFINITION HREF="m_defi_5.htm#define-condition"><B>define-condition</B></A>, or <A REL=DEFINITION HREF="m_defstr.htm#defstruct"><B>defstruct</B></A>. No other methods in the generic function are affected or replaced. <P><DD>
<DT>* If the given name 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 generic function is created with the methods specified by the method definitions in the <A REL=DEFINITION HREF="m_defgen.htm#defgeneric"><B>defgeneric</B></A> form. <P><DD>
<P></DL><P>
Some <A REL=DEFINITION HREF="26_glo_o.htm#operator"><I>operators</I></A> permit specification of the options of a <A REL=DEFINITION HREF="26_glo_g.htm#generic_function"><I>generic function</I></A>, such as the <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> of <A REL=DEFINITION HREF="26_glo_m.htm#method_combination"><I>method combination</I></A> it uses or its <A REL=DEFINITION HREF="26_glo_a.htm#argument_precedence_order"><I>argument precedence order</I></A>. These <A REL=DEFINITION HREF="26_glo_o.htm#operator"><I>operators</I></A> will be referred to as ``operators that specify generic function options.'' The only <A REL=DEFINITION HREF="26_glo_s.htm#standardized"><I>standardized</I></A> <A REL=DEFINITION HREF="26_glo_o.htm#operator"><I>operator</I></A> in this category is <A REL=DEFINITION HREF="m_defgen.htm#defgeneric"><B>defgeneric</B></A>. <P>
Some <A REL=DEFINITION HREF="26_glo_o.htm#operator"><I>operators</I></A> define <A REL=DEFINITION HREF="26_glo_m.htm#method"><I>methods</I></A> for a <A REL=DEFINITION HREF="26_glo_g.htm#generic_function"><I>generic function</I></A>. These <A REL=DEFINITION HREF="26_glo_o.htm#operator"><I>operators</I></A> will be referred to as <A REL=DEFINITION HREF="26_glo_m.htm#method-defining_operator"><I>method-defining operators</I></A>; their associated <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>forms</I></A> are called <A REL=DEFINITION HREF="26_glo_m.htm#method-defining_form"><I>method-defining forms</I></A>. The <A REL=DEFINITION HREF="26_glo_s.htm#standardized"><I>standardized</I></A> <A REL=DEFINITION HREF="26_glo_m.htm#method-defining_operator"><I>method-defining operators</I></A> are listed in the next figure. <A NAME="stdmethdefops"> <PRE>
<A REL=DEFINITION HREF="m_defgen.htm#defgeneric">defgeneric</A> <A REL=DEFINITION HREF="m_defmet.htm#defmethod">defmethod</A> <A REL=DEFINITION HREF="m_defcla.htm#defclass">defclass</A>
<A REL=DEFINITION HREF="m_defi_5.htm#define-condition">define-condition</A> <A REL=DEFINITION HREF="m_defstr.htm#defstruct">defstruct</A>
</PRE>
<P><B>Figure 7-1. Standardized Method-Defining Operators</B></A> Note that of the <A REL=DEFINITION HREF="26_glo_s.htm#standardized"><I>standardized</I></A> <A REL=DEFINITION HREF="26_glo_m.htm#method-defining_operator"><I>method-defining operators</I></A> only <A REL=DEFINITION HREF="m_defgen.htm#defgeneric"><B>defgeneric</B></A> can specify <A REL=DEFINITION HREF="26_glo_g.htm#generic_function"><I>generic function</I></A> options. <A REL=DEFINITION HREF="m_defgen.htm#defgeneric"><B>defgeneric</B></A> and any <A REL=DEFINITION HREF="26_glo_i.htm#implementation-defined"><I>implementation-defined</I></A> <A REL=DEFINITION HREF="26_glo_o.htm#operator"><I>operators</I></A> that can specify <A REL=DEFINITION HREF="26_glo_g.htm#generic_function"><I>generic function</I></A> options are also referred to as ``operators that specify generic function options.'' <P>
<P><HR>The following <A REL=META HREF="../Front/X3J13Iss.htm">X3J13 cleanup issues</A>, <I>not part of the specification</I>, apply to this section:<P><UL><LI> <A REL=CHILD HREF="../Issues/iss359.htm">WITH-ADDED-METHODS:DELETE</A><LI> <A REL=CHILD HREF="../Issues/iss181.htm">GENERIC-FLET-POORLY-DESIGNED:DELETE</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>