1
0
Fork 0
cl-sites/www.cs.cmu.edu/Groups/AI/html/cltl/clm/node278.html
2023-10-25 11:23:21 +02:00

122 lines
6.9 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.1. Introduction to Generic Functions</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Introduction to Generic Functions">
<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=tex2html5135 HREF="node279.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html5133 HREF="node277.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html5127 HREF="node277.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html5137 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html5138 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html5136 HREF="node279.html"> Introduction to Methods</A>
<B>Up:</B> <A NAME=tex2html5134 HREF="node277.html"> Generic Functions and </A>
<B> Previous:</B> <A NAME=tex2html5128 HREF="node277.html"> Generic Functions and </A>
<HR> <P>
<H3><A NAME=SECTION003216100000000000000>28.1.6.1. Introduction to Generic Functions</A></H3>
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
A generic function object contains a set of methods, a
lambda-list, a method combination type, and other information.
<P>
Like an ordinary Lisp function, a generic function takes arguments,
performs a series of operations, and perhaps returns useful values.
An ordinary function has a single body of code that is always executed
when the function is called. A generic function has a set of bodies
of code of which a subset is selected for execution. The selected
bodies of code and the manner of their combination are determined by
the classes or identities of one or more of the arguments to the
generic function and by its method combination type.
<P>
Ordinary functions and generic functions are called with identical function-call
syntax.
<P>
Generic functions are true functions that can be passed as arguments, returned as values,
used as the first argument to <tt>funcall</tt> and <tt>apply</tt>, and otherwise used in all the ways
an ordinary function may be used.
<P>
A name can be given to an ordinary function in one of
two ways: a <i>global</i> name can be given to a function using the
<tt>defun</tt> construct; a <i>local</i> name can be given using the
<tt>flet</tt> or <tt>labels</tt> special forms. A generic function can be
given a global name using the <tt>defmethod</tt> or <tt>defgeneric</tt>
construct. A generic function can be given a local name using the
<tt>generic-flet</tt>, <tt>generic-labels</tt>, or <tt>with-added-methods</tt>
special forms. The name of a generic function, like the name of an
ordinary function, can be either a symbol or a two-element list whose
first element is <tt>setf</tt> and whose second element is a symbol.
This is true for both local and global names.
<P>
The <tt>generic-flet</tt> special form creates new local generic
functions using the set of methods specified by the method definitions
in the <tt>generic-flet</tt> form. The scoping of generic function names
within a <tt>generic-flet</tt> form is the same as for <tt>flet</tt>.
<P>
The <tt>generic-labels</tt> special form creates a set of new mutually
recursive local generic functions using the set of methods specified
by the method definitions in the <tt>generic-labels</tt> form. The
scoping of generic function names within a <tt>generic-labels</tt> form
is the same as for <tt>labels</tt>.
<P>
The <tt>with-added-methods</tt> special form creates new local generic
functions by adding the set of methods specified by the method
definitions with a given name in the <tt>with-added-methods</tt> form to
copies of the methods of the lexically visible generic function of the
same name. If there is a lexically visible ordinary function of the
same name as one of the specified generic functions, that function
becomes the method function of the default method for the new generic
function of that name.
<P>
The <tt>generic-function</tt> macro creates an anonymous generic
function with the set of methods specified by the method definitions that appear
in the <tt>generic-function</tt> form.
<P>
When a <tt>defgeneric</tt> form is evaluated, one of three actions
is taken:
<P>
<UL><LI> If a generic function of the given name already exists,
the existing generic function object is modified. Methods specified
by the current <tt>defgeneric</tt> form are added, and any methods in the
existing generic function that were defined by a previous
<tt>defgeneric</tt> form are removed. Methods added by the current
<tt>defgeneric</tt> form might replace methods defined by <tt>defmethod</tt> or
<tt>defclass</tt>. No other methods in the generic function are affected
or replaced.
<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 definitions in the <tt>defgeneric</tt>
form.
<P>
</UL>
<P>
Some forms specify the options of a generic function,
such as the type of method combination it uses or its argument
precedence order. They will be referred to as ``forms that
specify generic function options.'' These forms are <tt>defgeneric</tt>,
<tt>generic-function</tt>, <tt>generic-flet</tt>, <tt>generic-labels</tt>, and
<tt>with-added-methods</tt>.
<P>
Some forms define methods for a generic function. They will be
referred to as ``method-defining forms.'' These forms are
<tt>defgeneric</tt>, <tt>defmethod</tt>, <tt>generic-function</tt>,
<tt>generic-flet</tt>, <tt>generic-labels</tt>, <tt>with-added-methods</tt>, and
<tt>defclass</tt>. Note that all the method-defining forms except
<tt>defclass</tt> and <tt>defmethod</tt>
are also forms that specify generic function options.
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
<BR> <HR><A NAME=tex2html5135 HREF="node279.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html5133 HREF="node277.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html5127 HREF="node277.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html5137 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html5138 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html5136 HREF="node279.html"> Introduction to Methods</A>
<B>Up:</B> <A NAME=tex2html5134 HREF="node277.html"> Generic Functions and </A>
<B> Previous:</B> <A NAME=tex2html5128 HREF="node277.html"> Generic Functions and </A>
<HR> <P>
<HR>
<P><ADDRESS>
AI.Repository@cs.cmu.edu
</ADDRESS>
</BODY>