

@node Objects, Structures, Iteration, Top
@chapter Objects

@menu
* Object Creation and Initialization::	
* Changing the Class of an Instance::  
* Reinitializing an Instance::	
* Meta-Objects::		
* Slots::			
* Generic Functions and Methods::  
* Objects Dictionary::		
@end menu

@node Object Creation and Initialization, Changing the Class of an Instance, Objects, Objects
@section Object Creation and Initialization

@c including concept-objects

The @i{generic function} @b{make-instance} creates and returns a new
@i{instance} of a @i{class}.  The first argument is a @i{class} or
the @i{name} of a @i{class}, and the remaining arguments form an 
@i{initialization argument list}
@IGindex initialization argument list
.

The initialization of a new @i{instance} consists of several distinct
steps, including the following: combining the explicitly supplied initialization
arguments with default values for the unsupplied initialization arguments, 
checking the validity of the initialization arguments, allocating storage 
for the @i{instance}, filling @i{slots} with
values, and executing user-supplied @i{methods} that perform additional
initialization.  Each step of @b{make-instance} is implemented by a
@i{generic function} to provide a mechanism for customizing that step.  
In addition, @b{make-instance} is itself a @i{generic function} 
and thus also can be customized.

The object system specifies system-supplied primary @i{methods} for each step 
and thus specifies a well-defined standard behavior for the entire
initialization process.  The standard behavior provides four simple
mechanisms for controlling initialization:

@table @asis

@item @t{*}  
Declaring a @i{symbol} to be an initialization argument 
for a @i{slot}.  An initialization argument is declared by using the
@t{:initarg} slot option to @b{defclass}.  This provides a mechanism
for supplying a value for a @i{slot} in a call to @b{make-instance}.

@item @t{*}  
Supplying a default value form for an initialization argument.
Default value forms for initialization arguments are defined by using the
@t{:default-initargs} class option to @b{defclass}.  If an 
initialization argument is not explicitly provided
as an argument to @b{make-instance}, the default value form is
evaluated in the lexical environment of the @b{defclass} form that
defined it, and the resulting value is used as the value of the
initialization argument.

@item @t{*}  
Supplying a default initial value form for a @i{slot}.  
A default initial value form for a @i{slot} is defined by using the 
@t{:initform} slot option to @b{defclass}.  If no initialization
argument associated with that @i{slot} is given as an argument to 
@b{make-instance} or is defaulted by @t{:default-initargs}, this
default initial value form is evaluated in the lexical environment of
the @b{defclass} form that defined it, and the resulting value is
stored in the @i{slot}.  The @t{:initform} form for a
@i{local slot} may be used when creating an @i{instance}, when 
updating an @i{instance} to conform to a redefined @i{class}, 
or when updating an @i{instance} to conform to the definition of a
different @i{class}. The @t{:initform} form for a
@i{shared slot} may be used when defining or re-defining the @i{class}.

@item @t{*}  
Defining @i{methods} for @b{initialize-instance} and
@b{shared-initialize}.  The slot-filling behavior described above is
implemented by a system-supplied primary @i{method} for
@b{initialize-instance} which invokes @b{shared-initialize}. The
@i{generic function} @b{shared-initialize} implements the parts of
initialization shared by these four situations: when making an @i{instance}, 
when re-initializing an @i{instance}, when updating an @i{instance}
to conform to a redefined @i{class}, and when updating an @i{instance} 
to conform to the definition of a different @i{class}. The system-supplied
primary @i{method} for @b{shared-initialize} directly implements the
slot-filling behavior described above, and @b{initialize-instance}
simply invokes @b{shared-initialize}.

@end table

@menu
* Initialization Arguments::	
* Declaring the Validity of Initialization Arguments::	
* Defaulting of Initialization Arguments::  
* Rules for Initialization Arguments::	
* Shared-Initialize::		
* Initialize-Instance::		
* Definitions of Make-Instance and Initialize-Instance::  
@end menu

@node Initialization Arguments, Declaring the Validity of Initialization Arguments, Object Creation and Initialization, Object Creation and Initialization
@subsection Initialization Arguments

An initialization argument controls @i{object} creation and
initialization.  It is often convenient to use keyword @i{symbols}
to name initialization arguments, but the @i{name} of an
initialization argument can be any @i{symbol}, including @b{nil}.  An
initialization argument can be used in two ways: to fill a @i{slot}
with a value or to provide an argument for an initialization
@i{method}.  A single initialization argument can be used for both
purposes.

An @i{initialization argument list} is a
@i{property list} of
initialization argument names and values.
Its structure is identical
to a @i{property list} and also 
to the portion of an argument list
processed for @b{&key} parameters.
As in those lists,
if an initialization
argument name appears more than once in an initialization argument list,
the leftmost occurrence supplies the value and the remaining occurrences
are ignored.  The arguments to @b{make-instance} (after the first
argument) form an @i{initialization argument list}.

An initialization argument can be associated with a @i{slot}.  If
the initialization argument has a value in the @i{initialization
argument list}, the value is stored into the @i{slot} of the newly
created @i{object}, overriding any @t{:initform} form associated
with the @i{slot}.  A single initialization argument can initialize
more than one @i{slot}.  An initialization argument that initializes
a @i{shared slot} stores its value into the @i{shared slot},
replacing any previous value.

An initialization argument can be associated with a @i{method}.  When
an @i{object} is created and a particular initialization argument is
supplied, the @i{generic functions} @b{initialize-instance},
@b{shared-initialize}, and @b{allocate-instance} are called
with that initialization argument's name and value as a keyword argument
pair.  If a value for the initialization argument is not supplied in the
@i{initialization argument list}, the @i{method}'s 
@i{lambda list} supplies a default value.

Initialization arguments are used in four situations: when making an
@i{instance}, when re-initializing an @i{instance}, when updating
an @i{instance} to conform to a redefined @i{class}, and when
updating an @i{instance} to conform to the definition of a different
@i{class}.

Because initialization arguments are used to control the creation and
initialization of an @i{instance} of some particular @i{class},
we say that an initialization argument is
``an initialization argument for'' that @i{class}.

@node Declaring the Validity of Initialization Arguments, Defaulting of Initialization Arguments, Initialization Arguments, Object Creation and Initialization
@subsection Declaring the Validity of Initialization Arguments

Initialization arguments are checked for validity in each of the four
situations that use them.  An initialization argument may be valid in
one situation and not another. For example, the system-supplied     
primary @i{method} for @b{make-instance} defined for 
the @i{class} @b{standard-class} checks the validity of its initialization arguments
and signals an error if an initialization argument is supplied that is
not declared as valid in that situation.

There are two means for declaring initialization arguments valid.

@table @asis

@item @t{*}  
Initialization arguments that fill @i{slots} are declared as valid
by the @t{:initarg} slot option to @b{defclass}.  The
@t{:initarg} slot option is inherited from @i{superclasses}.  Thus
the set of valid initialization arguments that fill @i{slots} for a
@i{class} is the union of the initialization arguments that fill
@i{slots} declared as valid by that @i{class} and its
@i{superclasses}.  Initialization arguments that fill @i{slots}
are valid in all four contexts.

@item @t{*}  
Initialization arguments that supply arguments to @i{methods} are
declared as valid by defining those @i{methods}.  The keyword name of
each keyword parameter specified in the @i{method}'s 
@i{lambda list} becomes an initialization argument for all @i{classes} 
for which the @i{method} is applicable.

The presence of @t{&allow-other-keys} in the
@i{lambda list} of an applicable method disables validity checking of 
initialization arguments.

Thus @i{method} inheritance
controls the set of valid initialization arguments that supply arguments
to @i{methods}.  The @i{generic functions} for which @i{method}
definitions serve to declare initialization arguments valid are as
follows:
@table @asis

@item --  
Making an @i{instance} of a @i{class}:
@b{allocate-instance}, @b{initialize-instance}, and
@b{shared-initialize}.  Initialization arguments declared as valid
by these @i{methods} are valid when making 
an @i{instance} of a @i{class}.

@item --  
Re-initializing an @i{instance}:
@b{reinitialize-instance} and @b{shared-initialize}.
Initialization arguments declared as valid by these @i{methods} are
valid when re-initializing an @i{instance}.

@item --  
Updating an @i{instance} to conform to a redefined @i{class}:
@b{update-instance-for-redefined-class} and @b{shared-initialize}.
Initialization arguments declared as valid by these @i{methods} are
valid when updating an @i{instance} to conform to a redefined @i{class}.

@item --  
Updating an @i{instance} to conform to the definition of a
different @i{class}:
@b{update-instance-for-different-class} and @b{shared-initialize}.
Initialization arguments declared as valid by these @i{methods} are
valid when updating an @i{instance} to conform to the definition
of a different @i{class}.

@end table

@end table

The set of valid initialization arguments for a @i{class} is the set of
valid initialization arguments that either fill @i{slots} or supply
arguments to @i{methods}, along with the predefined initialization
argument @t{:allow-other-keys}.  The default value for 
@t{:allow-other-keys} is @b{nil}.

Validity checking of initialization arguments is disabled if the value of
the initialization argument @t{:allow-other-keys} is @i{true}.

@node Defaulting of Initialization Arguments, Rules for Initialization Arguments, Declaring the Validity of Initialization Arguments, Object Creation and Initialization
@subsection Defaulting of Initialization Arguments

A default value @i{form} can be supplied for an initialization
argument by using the @t{:default-initargs} @i{class} option.  If an
initialization argument is declared valid by some particular @i{class},
its default  value form might be specified by a different @i{class}. 
In this case @t{:default-initargs} is used to supply a default value
for an inherited initialization argument.

The @t{:default-initargs} option is used only to provide default
values for initialization arguments; it does not declare a @i{symbol} 
as a valid initialization argument name.  Furthermore, 
the @t{:default-initargs} option is used only to provide default values for
initialization arguments when making an @i{instance}.

The argument to the @t{:default-initargs} class 
option is a list of
alternating initialization argument names and @i{forms}.  
Each @i{form} is the
default  value form for the corresponding initialization
argument.  The default  value @i{form} of an initialization
argument is used and evaluated only if that initialization argument
does not appear in the arguments to @b{make-instance} and is not
defaulted by a more specific @i{class}.  The default  value @i{form} is
evaluated in the lexical environment of the @b{defclass} form that
supplied it; the resulting value is used as the initialization
argument's value.

The initialization arguments supplied to @b{make-instance} are combined
with defaulted initialization arguments to produce a 
@i{defaulted initialization argument list}. A 
@i{defaulted initialization argument list}
is a list of alternating initialization argument names and
values in which unsupplied initialization arguments are defaulted and in
which the explicitly supplied initialization arguments appear earlier in
the list than the defaulted initialization arguments.  Defaulted
initialization arguments are ordered according to the order in the 
@i{class precedence list} of the @i{classes} that supplied the default values.

There is a distinction between the purposes of the 
@t{:default-initargs} and the @t{:initform} options with respect to the
initialization of @i{slots}.  The @t{:default-initargs} 
class option
provides a mechanism for the user to give a default  value @i{form}
for an initialization argument without knowing whether the
initialization argument initializes a @i{slot} 
or is passed to a @i{method}.
If that initialization argument is not explicitly supplied in a call
to @b{make-instance}, the default  value @i{form} is used, just
as if it had been supplied in the call.  In contrast, the 
@t{:initform} slot option provides a mechanism for the user to give a
default initial value form for a @i{slot}.  An @t{:initform} form is
used to initialize a @i{slot} only if no initialization argument
associated with that @i{slot} is given as an argument to 
@b{make-instance} or is defaulted by @t{:default-initargs}.

@ITindex order of evaluation

@ITindex evaluation order

The order of evaluation of default value @i{forms} for initialization
arguments and the order of evaluation of @t{:initform} forms are
undefined.  If the order of evaluation is important, 
@b{initialize-instance} or @b{shared-initialize} @i{methods} 
should be used
instead.

@node Rules for Initialization Arguments, Shared-Initialize, Defaulting of Initialization Arguments, Object Creation and Initialization
@subsection Rules for Initialization Arguments

The @t{:initarg} slot option may be specified more than
once for a given @i{slot}.

The following rules specify when initialization arguments may be
multiply defined:

@table @asis

@item @t{*}  
A given initialization argument can be used to
initialize more than one @i{slot} if the same initialization argument name
appears in more than one @t{:initarg} slot option.

@item @t{*}  
A given initialization argument name can appear 
in the @i{lambda list} of more than one initialization @i{method}.

@item @t{*}  
A given initialization argument name can
appear both in an @t{:initarg} slot option and 
in the @i{lambda list}
of an initialization @i{method}.

@end table

[Reviewer Note by The next three paragraphs could be replaced by ``If two or more
initialization arguments that initialize the same slot appear in the
@i{defaulted initialization argument list}, the leftmost of these supplies
the value, even if they have different names.''  And the rest would follow
from the rules above.]

If two or more initialization arguments that initialize the same
@i{slot} are given in the arguments to @b{make-instance}, the
leftmost of these initialization arguments in the @i{initialization
argument list} supplies the value, even if the initialization arguments
have different names.

If two or more different initialization arguments that initialize the
same @i{slot} have default values and none is given explicitly in the
arguments to @b{make-instance}, the initialization argument that
appears in a @t{:default-initargs} class option in the most specific
of the @i{classes} supplies the value. If a single
@t{:default-initargs} class option specifies two or more initialization
arguments that initialize the same @i{slot} and none is given
explicitly in the arguments to @b{make-instance}, the leftmost in
the @t{:default-initargs} class option supplies the value, and the
values of the remaining default value @i{forms} are ignored.

Initialization arguments given explicitly in the arguments to
@b{make-instance} appear to the left of defaulted initialization
arguments. Suppose that the classes C_1 and C_2 supply the
values of defaulted initialization arguments for different @i{slots},
and suppose that C_1 is more specific than C_2; then the
defaulted initialization argument whose value is supplied by C_1
is to the left of the defaulted initialization argument whose value is
supplied by C_2 in the @i{defaulted initialization argument
list}.  If a single @t{:default-initargs} class option supplies the
values of initialization arguments for two different @i{slots}, the
initialization argument whose value is specified farther to the left in
the @t{:default-initargs} class option appears farther to the left in
the @i{defaulted initialization argument list}.

[Reviewer Note by Barmar: End of claim made three paragraphs back.]

If a @i{slot} has both an @t{:initform} form and an 
@t{:initarg} slot option, and the initialization argument is defaulted
using @t{:default-initargs} or is supplied to @b{make-instance},
the captured @t{:initform} form is neither used nor evaluated.

The following is an example of the above rules:

@example
 (defclass q () ((x :initarg a)))
 (defclass r (q) ((x :initarg b))
   (:default-initargs a 1 b 2))
@end example

@example
@format
@group
@noindent
@w{ @t{}                           Defaulted                    @t{}                 }
@w{ Form                         Initialization Argument List Contents of Slot X }
@w{ _____________________________________________________________________________}
@w{ @t{(make-instance 'r)}           @t{(a 1 b 2)}                    @t{1}                  }
@w{ @t{(make-instance 'r 'a 3)}      @t{(a 3 b 2)}                    @t{3}                  }
@w{ @t{(make-instance 'r 'b 4)}      @t{(b 4 a 1)}                    @t{4}                  }
@w{ @t{(make-instance 'r 'a 1 'a 2)} @t{(a 1 a 2 b 2)}                @t{1}                  }
@end group
@end format

@end example


@node Shared-Initialize, Initialize-Instance, Rules for Initialization Arguments, Object Creation and Initialization
@subsection Shared-Initialize

The @i{generic function} @b{shared-initialize} is used to fill the 
@i{slots}
of an @i{instance} 
using initialization arguments and @t{:initform}
forms when an @i{instance} is created, when an 
@i{instance} is re-initialized,
when an @i{instance} 
is updated to conform to a redefined @i{class}, and when
an @i{instance} is updated to conform to a different @i{class}.
It uses
standard @i{method} combination. It takes the following arguments: the
@i{instance} to be initialized, a 
specification of a set of @i{names} of @i{slots}
@i{accessible} in that @i{instance}, and any number of initialization
arguments.  The arguments after the first two must form an
@i{initialization argument list}.

The second argument to @b{shared-initialize} may be one of the following:

@table @asis

@item @t{*}  
It can be a (possibly empty) @i{list} of @i{slot} names,
which specifies the set of those @i{slot} names. 

@item @t{*}  
It can be the symbol @b{t}, which specifies the set of all of the @i{slots}.

@end table

There is a system-supplied primary @i{method} for @b{shared-initialize}
whose first @i{parameter specializer} is the @i{class} @b{standard-object}.
This @i{method} behaves as follows on each @i{slot}, 
whether shared or local:

@table @asis

@item @t{*}  
If an initialization argument in the 
@i{initialization argument list} specifies a value for that @i{slot}, 
that value is stored
into the @i{slot}, even if a value has already been stored in the @i{slot}
before the @i{method} is run.  
The affected @i{slots} are independent of which
@i{slots} are indicated by the second argument to @b{shared-initialize}.

@item @t{*}  
Any @i{slots} 
indicated by the second argument that are still
unbound at this point are initialized according to their 
@t{:initform} forms.  For any such @i{slot} 
that has an @t{:initform} form,
that @i{form} is evaluated in the 
lexical environment of its defining 
@b{defclass} form and the result is stored into the @i{slot}.  
For example,
if a @i{before method} stores a value in the 
@i{slot}, the @t{:initform} form will not be used to supply a value 
for the @i{slot}.  If
the second argument specifies a @i{name} that does not correspond to any
@i{slots} @i{accessible} 
in the @i{instance}, the results are unspecified.

@item @t{*}  
The rules mentioned in @ref{Rules for Initialization Arguments} are obeyed.

@end table

The generic function @b{shared-initialize} is called by the
system-supplied primary @i{methods} 
for @b{reinitialize-instance},
@b{update-instance-for-different-class}, 
@b{update-instance-for-redefined-class}, and 
@b{initialize-instance}.  Thus, @i{methods} can be written for 
@b{shared-initialize} to specify actions that should be taken in all of
these contexts.

@node Initialize-Instance, Definitions of Make-Instance and Initialize-Instance, Shared-Initialize, Object Creation and Initialization
@subsection Initialize-Instance

The @i{generic function} @b{initialize-instance} is called by 
@b{make-instance} to initialize a newly created @i{instance}.
It uses @i{standard method combination}.  @i{Methods} for 
@b{initialize-instance} can be defined in order to perform any
initialization that cannot be achieved 
simply by supplying initial values for @i{slots}.

During initialization, @b{initialize-instance} is invoked
after the following actions have been taken:

@table @asis

@item @t{*}  
The @i{defaulted initialization argument list} 
has been computed by combining the supplied @i{initialization argument list} 
with any default initialization arguments for the @i{class}.

@item @t{*}  
The validity of the @i{defaulted initialization argument list}
has been checked.  If any of the initialization arguments has not
been declared as valid, an error is signaled. 

@item @t{*}  
A new @i{instance} whose @i{slots} 
are unbound has been created.

@end table

The generic function @b{initialize-instance} is called with the
new @i{instance} and the defaulted initialization arguments.  There is
a system-supplied primary @i{method} for @b{initialize-instance}
whose @i{parameter specializer} is the @i{class} @b{standard-object}.  This
@i{method} calls the generic function 
@b{shared-initialize} to fill in
the @i{slots} according to the initialization arguments and the 
@t{:initform} forms for the @i{slots}; the generic function 
@b{shared-initialize} is called with the following arguments: the @i{instance},
@b{t}, and the defaulted initialization arguments.

Note that @b{initialize-instance} provides the 
@i{defaulted initialization argument list} in its call to @b{shared-initialize},
so the first step performed by the system-supplied primary @i{method} for
@b{shared-initialize} takes into account both the initialization
arguments provided in the call to @b{make-instance} and the
@i{defaulted initialization argument list}.

@i{Methods} for @b{initialize-instance} can be defined to specify
actions to be taken when an @i{instance} is initialized.  
If only @i{after methods} for @b{initialize-instance} are defined, they will be
run after the system-supplied primary @i{method} for initialization and
therefore will not interfere with the default behavior of 
@b{initialize-instance}.

The object system provides two @i{functions} that are useful in the bodies of 
@b{initialize-instance} methods.  The @i{function} @b{slot-boundp}
returns a @i{generic boolean} value that indicates whether a specified @i{slot} has a
value; this provides a mechanism for writing @i{after methods} for
@b{initialize-instance} that initialize @i{slots} only if they have
not already been initialized.  The @i{function} @b{slot-makunbound}
causes the @i{slot} to have no value.

@node Definitions of Make-Instance and Initialize-Instance,  , Initialize-Instance, Object Creation and Initialization
@subsection Definitions of Make-Instance and Initialize-Instance

The generic function @b{make-instance} behaves as if it were defined as
follows, except that certain optimizations are permitted:

@example
 (defmethod make-instance ((class standard-class) &rest initargs)
   ...
   (let ((instance (apply #'allocate-instance class initargs)))
     (apply #'initialize-instance instance initargs)
     instance))

 (defmethod make-instance ((class-name symbol) &rest initargs)
   (apply #'make-instance (find-class class-name) initargs))
@end example

The elided code in the definition of @b{make-instance} 
augments the @t{initargs} with any @i{defaulted initialization arguments} and
checks the
resulting
initialization arguments to determine whether an initialization
argument was supplied that neither filled a @i{slot} nor supplied an argument
to an applicable @i{method}. 

The generic function @b{initialize-instance} behaves as if it were
defined as follows, except that certain optimizations are permitted:

@example
 (defmethod initialize-instance ((instance standard-object) &rest initargs)
   (apply #'shared-initialize instance t initargs)))
@end example

These procedures can be customized.

Customizing at the Programmer Interface level includes using the 
@t{:initform}, @t{:initarg}, and @t{:default-initargs} options to
@b{defclass}, as well as defining @i{methods}
for @b{make-instance}, 
@b{allocate-instance},
and @b{initialize-instance}.  It is also possible to define
@i{methods} for @b{shared-initialize}, which would be invoked by the
generic functions @b{reinitialize-instance}, 
@b{update-instance-for-redefined-class}, 
@b{update-instance-for-different-class}, and 
@b{initialize-instance}.  
The meta-object level supports additional
customization.

Implementations are permitted to make certain optimizations to 
@b{initialize-instance} and @b{shared-initialize}.  
The description of @b{shared-initialize} in Chapter~7 mentions the
possible optimizations.

@c end of including concept-objects

@node Changing the Class of an Instance, Reinitializing an Instance, Object Creation and Initialization, Objects
@section Changing the Class of an Instance

@c including concept-change-class

The @i{function} @b{change-class} can be used to change the @i{class} 
of an @i{instance} from its current class, C_@{@r{from}@},
to a different class, C_@{@r{to}@}; it changes the
structure of the @i{instance} to conform to the definition of the class
C_@{@r{to}@}.

Note that changing the @i{class} of an @i{instance} may cause
@i{slots} to be added or deleted.  Changing the @i{class} of an
@i{instance} does not change its identity as defined by the
@b{eq} function.

When @b{change-class} is invoked on an @i{instance}, a two-step
updating process takes place.  The first step modifies the structure of
the @i{instance} by adding new @i{local slots} and discarding 
@i{local slots} that are not specified in the new version of the @i{instance}.
The second step initializes the newly added @i{local slots} and performs 
any other user-defined actions. These two steps are further described in the 
two following sections.

@menu
* Modifying the Structure of the Instance::  
* Initializing Newly Added Local Slots (Changing the Class of an Instance)::  
* Customizing the Change of Class of an Instance::  
@end menu

@node Modifying the Structure of the Instance, Initializing Newly Added Local Slots (Changing the Class of an Instance), Changing the Class of an Instance, Changing the Class of an Instance
@subsection Modifying the Structure of the Instance

In order to make the @i{instance} conform to the class C_@{@r{to}@}, @i{local slots} specified by the class C_@{@r{to}@} that are not specified by the class C_@{@r{from}@} are added, and @i{local slots} not specified by
the class C_@{@r{to}@} that are specified by the
class C_@{@r{from}@} are discarded.

The values of @i{local slots} specified by both the class C_@{@r{to}@} and the class C_@{@r{from}@} are retained. If such a @i{local slot} was unbound, it remains
unbound.

The values of @i{slots} specified as shared in the class C_@{@r{from}@} and as local in the class C_@{@r{to}@} are retained.

This first step of the update does not affect the values of any 
@i{shared slots}.

@node Initializing Newly Added Local Slots (Changing the Class of an Instance), Customizing the Change of Class of an Instance, Modifying the Structure of the Instance, Changing the Class of an Instance
@subsection Initializing Newly Added Local Slots

The second step of the update initializes the newly added @i{slots} and
performs any other user-defined actions.  This step is implemented by
the generic function @b{update-instance-for-different-class}.  The
generic function @b{update-instance-for-different-class} is invoked
by @b{change-class} after the first step of the update has been
completed.

The generic function @b{update-instance-for-different-class} is
invoked on arguments computed by @b{change-class}.
The first argument passed is a copy of the @i{instance} being updated 
and is an @i{instance} of the class C_@{@r{from}@}; 
this copy has @i{dynamic extent} within the generic function @b{change-class}.  
The second argument is the @i{instance} as updated so far by @b{change-class}
and is an @i{instance} of the class C_@{@r{to}@}.
The remaining arguments are an @i{initialization argument list}.

There is a system-supplied primary @i{method} for 
@b{update-instance-for-different-class} that has two parameter
specializers, each of which is the @i{class} @b{standard-object}.  First
this @i{method} checks the validity of initialization arguments and
signals an error if an initialization argument is supplied that is not
declared as valid.  (For more information, see @ref{Declaring the Validity of Initialization Arguments}.)
Then it calls the
generic function @b{shared-initialize} with the following arguments:
the
new
@i{instance}, a list of @i{names} of the newly added 
@i{slots}, and the
initialization arguments it received.

@node Customizing the Change of Class of an Instance,  , Initializing Newly Added Local Slots (Changing the Class of an Instance), Changing the Class of an Instance
@subsection Customizing the Change of Class of an Instance

@i{Methods} for @b{update-instance-for-different-class} may be defined
to specify actions to be taken when an @i{instance} is updated.  If only
@i{after methods} for @b{update-instance-for-different-class} are
defined, they will be run after the system-supplied primary @i{method} for
initialization and will not interfere with the default behavior of
@b{update-instance-for-different-class}.

@i{Methods} 
for @b{shared-initialize} may be defined to customize @i{class}
redefinition.  For more information, see @ref{Shared-Initialize}.

@c end of including concept-change-class

@node Reinitializing an Instance, Meta-Objects, Changing the Class of an Instance, Objects
@section Reinitializing an Instance

@c including concept-reinit

The generic function @b{reinitialize-instance} may be used to change
the values of @i{slots} according to initialization arguments.

The process of reinitialization changes the values of some @i{slots} and
performs any user-defined actions.  It does not modify the structure
of an @i{instance} to add or delete @i{slots}, 
and it does not use any @t{:initform} forms to initialize @i{slots}.

The generic function @b{reinitialize-instance} may be called
directly.  It takes one required argument, the @i{instance}.  It also
takes any number of initialization arguments to be used by @i{methods} for
@b{reinitialize-instance} or for @b{shared-initialize}. The
arguments after the required @i{instance} must form an 
@i{initialization argument list}.

There is a system-supplied primary @i{method} for 
@b{reinitialize-instance} whose @i{parameter specializer} is 
the @i{class} @b{standard-object}.  First this @i{method} checks the validity of
initialization arguments and signals an error if an initialization
argument is supplied that is not declared as valid. 
(For more information, see @ref{Declaring the Validity of Initialization Arguments}.)
Then it calls the generic function 
@b{shared-initialize} with the following arguments: the @i{instance},
@b{nil}, and the initialization arguments it received.

@menu
* Customizing Reinitialization::  
@end menu

@node Customizing Reinitialization,  , Reinitializing an Instance, Reinitializing an Instance
@subsection Customizing Reinitialization

@i{Methods} for @b{reinitialize-instance} may be defined to specify
actions to be taken when an @i{instance} is updated.  If only
@i{after methods} for @b{reinitialize-instance} are defined, 
they will be run after the system-supplied primary @i{method} for 
initialization and therefore will not interfere with the default behavior of 
@b{reinitialize-instance}.

@i{Methods} for @b{shared-initialize} may be defined to customize 
@i{class} redefinition.  For more information, see @ref{Shared-Initialize}.

@c end of including concept-reinit

@node Meta-Objects, Slots, Reinitializing an Instance, Objects
@section Meta-Objects

@c including concept-meta-objects

The implementation of the object system manipulates @i{classes}, @i{methods},
and @i{generic functions}.  The object system contains a set of 
@i{generic functions} defined by @i{methods} on @i{classes}; 
the behavior of those @i{generic functions} defines the behavior of
the object system.  The @i{instances} of the @i{classes} on which those
@i{methods} are defined are called meta-objects.  

@menu
* Standard Meta-objects::	
@end menu

@node Standard Meta-objects,  , Meta-Objects, Meta-Objects
@subsection Standard Meta-objects

The object system supplies a set of meta-objects, called standard meta-objects.
These include the @i{class} @b{standard-object} and
@i{instances} of the classes @b{standard-method}, 
@b{standard-generic-function}, and @b{method-combination}.

@table @asis

[Editorial Note by KMP: This is said redundantly in the definition of STANDARD-METHOD.]
@item @t{*}  
The @i{class} @b{standard-method} is the default @i{class} of 
@i{methods} defined by the 
 @b{defmethod} and
 @b{defgeneric} @i{forms}.

@item @t{*}  
The @i{class} @b{standard-generic-function} is the default @i{class} of 
@i{generic functions} defined by the forms
  @b{defmethod},
  @b{defgeneric},

 and
  @b{defclass}.

@item @t{*}  
The @i{class} named @b{standard-object} 
is an @i{instance} of the @i{class} @b{standard-class} 
and is a @i{superclass} of every @i{class} that is an
@i{instance} of @b{standard-class} except itself and 
@b{structure-class}.

@item @t{*}  
Every @i{method} combination object is 
an @i{instance} of a @i{subclass} of @i{class} @b{method-combination}.

@end table

@c end of including concept-meta-objects

@node Slots, Generic Functions and Methods, Meta-Objects, Objects
@section Slots

@c including concept-slots

@menu
* Introduction to Slots::	
* Accessing Slots::		
* Inheritance of Slots and Slot Options::  
@end menu

@node Introduction to Slots, Accessing Slots, Slots, Slots
@subsection Introduction to Slots

An @i{object} of @i{metaclass} @b{standard-class} has zero or more named
@i{slots}.  The @i{slots} of an @i{object} are determined 
by the @i{class} of the @i{object}.  Each @i{slot} can hold
one value.

[Reviewer Note by Barmar: All symbols are valid variable names.  Perhaps this means
                  to preclude the use of named constants?  We have a terminology
		  problem to solve.]
The @i{name} of a @i{slot} is a @i{symbol} that is syntactically
valid for use as a variable name.

When a @i{slot} does not have a value, the @i{slot} is said to be 
@i{unbound}.  When an unbound @i{slot} is read,

[Reviewer Note by Barmar: from an object whose metaclass is standard-class?]
the @i{generic function} @b{slot-unbound} is invoked. The 
system-supplied primary @i{method} 
for @b{slot-unbound} 
on @i{class} @b{t} signals an error.

If @b{slot-unbound} returns, its @i{primary value} 
is used that time as the @i{value} of the @i{slot}.

The default initial value form for a @i{slot} is defined by
the @t{:initform} slot option.  When the @t{:initform} form is used to
supply a value, it is evaluated in the lexical environment in which
the @b{defclass} form was evaluated. The @t{:initform} along with
the lexical environment in which the @b{defclass} form was evaluated
is called a @i{captured initialization form}. 
For more details, see @ref{Object Creation and Initialization}.

A @i{local slot} is defined to be a @i{slot} that is
@i{accessible}
to exactly one @i{instance}, 
namely the one in which the @i{slot} is allocated.  
A @i{shared slot} is defined to be a @i{slot} that is visible to more than one
@i{instance} of a given @i{class} and its @i{subclasses}.

A @i{class} is said to define a @i{slot} with a given @i{name} when
the @b{defclass} form for that @i{class} contains a @i{slot specifier} with
that @i{name}.  Defining a @i{local slot} does not immediately create 
a @i{slot}; it causes a @i{slot} to be created each time 
an @i{instance} of the @i{class} is created.
Defining a @i{shared slot} immediately creates a @i{slot}.

The @t{:allocation} slot option to @b{defclass} controls the kind
of @i{slot} that is defined.  If the value of the @t{:allocation} slot
option is @t{:instance}, a @i{local slot} is created.  If the value of
@t{:allocation} is @t{:class}, a @i{shared slot} is created.

A @i{slot} is said to be @i{accessible} in an @i{instance} 
of a @i{class} if
the @i{slot} is defined by the @i{class} 
of the @i{instance} or is inherited from
a @i{superclass} of that @i{class}.  
At most one @i{slot} of a given @i{name} can be
@i{accessible} in an @i{instance}.  
A @i{shared slot} defined by a @i{class} is
@i{accessible} in all @i{instances} 
of that @i{class}.  
A detailed explanation of the inheritance of @i{slots} is given in 
@ref{Inheritance of Slots and Slot Options}.

@node Accessing Slots, Inheritance of Slots and Slot Options, Introduction to Slots, Slots
@subsection Accessing Slots

@i{Slots} can be @i{accessed} in two ways: by use of the primitive function
@b{slot-value} and by use of @i{generic functions} generated by
the @b{defclass} form.

The @i{function} @b{slot-value} can be used with any of the @i{slot}
names specified in the @b{defclass} form to @i{access} a specific
@i{slot} @i{accessible} in an @i{instance} of the given @i{class}.

The macro @b{defclass} provides syntax for generating @i{methods} to
read and write @i{slots}.  If a reader @i{method} is requested, 
a @i{method} is automatically generated for reading the value of the
@i{slot}, but no @i{method} for storing a value into it is generated.
If a writer @i{method} is requested, a @i{method} is automatically 
generated for storing a value into the @i{slot}, but no @i{method} 
for reading its value is generated.  If an accessor @i{method} is 
requested, a @i{method} for reading the value of the @i{slot} and a
@i{method} for storing a value into the @i{slot} are automatically
generated.  Reader and writer @i{methods} are implemented using
@b{slot-value}.

When a reader or writer @i{method} is specified for a @i{slot}, the
name of the @i{generic function} to which the generated @i{method}
belongs is directly specified.  If the @i{name} specified for the writer
@i{method} is the symbol @t{name}, the @i{name} of the
@i{generic function} for writing the @i{slot} is the symbol
@t{name}, and the @i{generic function} takes two arguments: the new
value and the @i{instance}, in that order.  If the @i{name} specified
for the accessor @i{method} is the symbol @t{name}, the @i{name} of
the @i{generic function} for reading the @i{slot} is the symbol 
@t{name}, and the @i{name} of the @i{generic function} for writing 
the @i{slot} is the list @t{(setf name)}.

A @i{generic function} created or modified by supplying @t{:reader},
@t{:writer}, or @t{:accessor} @i{slot} options can be treated exactly
as an ordinary @i{generic function}.

Note that @b{slot-value} can be used to read or write the value of a
@i{slot} whether or not reader or writer @i{methods} exist for that
@i{slot}.  When @b{slot-value} is used, no reader or writer
@i{methods} are invoked.

The macro @b{with-slots} can be used to establish a 
@i{lexical environment} in which specified @i{slots} are lexically
available as if they were variables.  The macro @b{with-slots} 
invokes the @i{function} @b{slot-value} to @i{access} the specified @i{slots}.

The macro @b{with-accessors} can be used to establish a lexical
environment in which specified @i{slots} are lexically available through
their accessors as if they were variables.  The macro @b{with-accessors}
invokes the appropriate accessors to @i{access} the specified @i{slots}. 

@node Inheritance of Slots and Slot Options,  , Accessing Slots, Slots
@subsection Inheritance of Slots and Slot Options

The set of the @i{names} of all @i{slots} @i{accessible} 
in an @i{instance} of a @i{class} C is the union of 
the sets of @i{names} of @i{slots} defined by C and its
@i{superclasses}. The structure of an @i{instance} is
the set of @i{names} of @i{local slots} in that @i{instance}.

In the simplest case, only one @i{class} among C and its @i{superclasses}
defines a @i{slot} with a given @i{slot} name.  
If a @i{slot} is defined by a @i{superclass} of C, 
the @i{slot} is said to be inherited.  The characteristics 
of the @i{slot} are determined by the @i{slot specifier}
of the defining @i{class}.
Consider the defining @i{class} for
a slot S.  If the value of the @t{:allocation} 
slot
option is @t{:instance}, then S is a @i{local slot} and each 
@i{instance}
of C has its own @i{slot} named S that stores its own value.  If the
value of the @t{:allocation} slot 
option is @t{:class}, then S
is a @i{shared slot}, the @i{class} 
that defined S stores the value, and all
@i{instances} of C can @i{access} that single @i{slot}.  
If the @t{:allocation} slot option is omitted, @t{:instance} is used.

In general, more than one @i{class} among C and its 
@i{superclasses} can
define a @i{slot} with a given @i{name}.  
In such cases, only one @i{slot} with
the given name is @i{accessible} in an @i{instance} 
of C, and
the characteristics of that @i{slot} are 
a combination of the several @i{slot}
specifiers, computed as follows:

@table @asis

@item @t{*}  
All the @i{slot specifiers} for a given @i{slot} name
are ordered from most specific to least specific, according to the order in C's
@i{class precedence list} of the @i{classes} that define them. All references
to the specificity of @i{slot specifiers} immediately below refers to this
ordering.

@item @t{*}  
The allocation of a @i{slot} is controlled by the most 
specific @i{slot specifier}.  If the most specific @i{slot specifier} 
does not contain an @t{:allocation} slot option, @t{:instance} is used.
Less specific @i{slot specifiers} do not affect the allocation.

@item @t{*}  
The default initial value form for a @i{slot} 
is the value of the @t{:initform} slot option in the most specific
@i{slot specifier} that contains one.  If no @i{slot specifier}
contains an @t{:initform} slot option, the @i{slot} 
has no default initial value form.

@item @t{*}  
The contents of a @i{slot} will always be of type 
@t{(and T_1 ... T_n)} where T_1 ... T_n are
the values of the @t{:type} slot options contained in all of the
@i{slot specifiers}.  If no @i{slot specifier} contains the
@t{:type} slot option, the contents of the @i{slot} will always be 
of @i{type} @b{t}. The consequences of attempting to store in a @i{slot}
a value that does not satisfy the @i{type} of the @i{slot} are undefined.

@item @t{*}  
The set of initialization arguments that initialize a 
given @i{slot} is the union of the initialization arguments declared in
the @t{:initarg} slot options in all the @i{slot specifiers}.

@item @t{*}  
The @i{documentation string} for a @i{slot} is the value of
the @t{:documentation} slot option in the most specific @i{slot}
specifier that contains one.  If no @i{slot specifier} contains a
@t{:documentation} slot option, the @i{slot} has no @i{documentation string}.

@end table

A consequence of the allocation rule is that a @i{shared slot} can be
@i{shadowed}.  For example, if a class C_1 defines 
a @i{slot} named S
whose value for the @t{:allocation} slot option is @t{:class},
that @i{slot} is @i{accessible} 
in @i{instances} of C_1 and all of its
@i{subclasses}.  However, if C_2 is a @i{subclass} 
of C_1 and also
defines a @i{slot} named S, C_1's 
@i{slot} is not shared
by @i{instances} of C_2 and its @i{subclasses}. When a class
C_1 defines a @i{shared slot}, any subclass C_2 of C_1 will share this single @i{slot} 
unless the @b{defclass} form for
C_2 specifies a @i{slot} of the same 
@i{name} or there is a @i{superclass}
of C_2 that precedes C_1 in the @i{class precedence list} of
C_2 that defines a @i{slot} of the same name.

A consequence of the type rule is that the value of a @i{slot}
satisfies the type constraint of each @i{slot specifier} that
contributes to that @i{slot}.  Because the result of attempting to
store in a @i{slot} a value that does not satisfy the type
constraint for the @i{slot} is undefined, the value in a @i{slot}
might fail to satisfy its type constraint.

The @t{:reader}, @t{:writer}, and @t{:accessor} slot options
create @i{methods} rather than define the characteristics of a @i{slot}.
Reader and writer @i{methods} are inherited in the sense described in
@ref{Inheritance of Methods}.

@i{Methods} that @i{access} @i{slots} use only the name of the
@i{slot} and the @i{type} of the @i{slot}'s value.  Suppose
a @i{superclass} provides a @i{method} that expects to @i{access} a
@i{shared slot} of a given @i{name}, and a @i{subclass} defines
a @i{local slot} with the same @i{name}.  If the @i{method} provided 
by the @i{superclass} is used on an @i{instance} of the @i{subclass}, 
the @i{method} @i{accesses} the @i{local slot}.

@c end of including concept-slots

@node Generic Functions and Methods, Objects Dictionary, Slots, Objects
@section Generic Functions and Methods

@c including concept-gfs-and-methods

@menu
* Introduction to Generic Functions::  
* Introduction to Methods::	
* Agreement on Parameter Specializers and Qualifiers::	
* Congruent Lambda-lists for all Methods of a Generic Function::  
* Keyword Arguments in Generic Functions and Methods::	
* Method Selection and Combination::  
* Inheritance of Methods::	
@end menu

@node Introduction to Generic Functions, Introduction to Methods, Generic Functions and Methods, Generic Functions and Methods
@subsection Introduction to Generic Functions

A @i{generic function}
@IGindex generic function
 is a function whose behavior depends on
the @i{classes} or identities of the @i{arguments} supplied to it.
A @i{generic function} @i{object} 
is associated with 
     a set of @i{methods},
     a @i{lambda list},
     a @i{method combination}_2, 
 and other information.

Like an @i{ordinary function}, a @i{generic function} takes @i{arguments},
performs a series of operations, and perhaps returns useful @i{values}.
An @i{ordinary function} has a single body of @i{code} that is always @i{executed}
when the @i{function} is called.  A @i{generic function} has a set of bodies
of @i{code} of which a subset is selected for @i{execution}. The selected
bodies of @i{code} and the manner of their combination are determined by
the @i{classes} or identities of one or more of the @i{arguments} to the
@i{generic function} and by its @i{method combination}.

@i{Ordinary functions} and @i{generic functions} are called with identical syntax.

@i{Generic functions} are true @i{functions} that can be passed as @i{arguments}
and used as the first @i{argument} to @b{funcall} and @b{apply}.

A @i{binding} of a @i{function name} to a @i{generic function}
can be @i{established} in one of several ways.  It can be
@i{established} in the @i{global environment} by 
 @b{ensure-generic-function},
 @b{defmethod} (implicitly, due to @b{ensure-generic-function})
or
 @b{defgeneric} (also implicitly, due to @b{ensure-generic-function}).

No @i{standardized} mechanism is provided for @i{establishing} a
@i{binding} of a @i{function name} to a @i{generic function}
in the @i{lexical environment}.

When a @b{defgeneric} form is evaluated, one of three actions
is taken (due to @b{ensure-generic-function}):

@table @asis

@item @t{*}  
If a generic function of the given name already exists,
the existing generic function object is modified.  Methods specified
by the current @b{defgeneric} form are added, and any methods in the
existing generic function that were defined by a previous @b{defgeneric}
form are removed.  Methods added by the current @b{defgeneric} 
form might replace methods defined by @b{defmethod}, 
@b{defclass}, @b{define-condition}, or @b{defstruct}.  
No other methods in the generic function are affected
or replaced.

@item @t{*}  
If the given name names 
    an @i{ordinary function}, 
    a  @i{macro},
 or a @i{special operator}, 
an error is signaled.

@item @t{*}  
Otherwise a generic function is created with the
methods specified by the method definitions in the @b{defgeneric}
form.

@end table

Some @i{operators} permit specification of the options of a
@i{generic function}, such as 
the @i{type} of @i{method combination} it uses 
or its @i{argument precedence order}.
These @i{operators} will be referred to as
``operators that specify generic function options.''

The only @i{standardized} @i{operator} in this category is @b{defgeneric}.

Some @i{operators} define @i{methods} for a @i{generic function}.
These @i{operators} will be referred to as
@i{method-defining operators}
@IGindex method-defining operator
;
their associated @i{forms} are called @i{method-defining forms}.
The @i{standardized} @i{method-defining operators} are listed in Figure 7--2.

@format
@group
@noindent
@w{  defgeneric        defmethod  defclass  }
@w{  define-condition  defstruct            }

@noindent
@w{  Figure 7--2: Standardized Method-Defining Operators}

@end group
@end format

Note that of the @i{standardized} @i{method-defining operators}
only @b{defgeneric}
can specify @i{generic function} options.
@b{defgeneric} and any @i{implementation-defined} @i{operators}
that can specify @i{generic function} options
are also referred to as ``operators that specify generic function options.''

@node Introduction to Methods, Agreement on Parameter Specializers and Qualifiers, Introduction to Generic Functions, Generic Functions and Methods
@subsection Introduction to Methods

@i{Methods} define the class-specific or identity-specific behavior
and operations of a @i{generic function}. 

A @i{method} @i{object} 
is associated with 
     @i{code} that implements the method's behavior,
     a sequence of @i{parameter specializers} 
       that specify when the given @i{method} is applicable,
     a @i{lambda list},
 and a sequence of @i{qualifiers} that are used by the method combination
       facility to distinguish among @i{methods}.

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.

A method-defining form contains the @i{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:

@table @asis

@item @t{*}  
If a @i{generic function} of the given name already exists
and if a @i{method object} already exists that agrees with the new one on
@i{parameter specializers} and @i{qualifiers}, the new @i{method object} replaces
the old one.  For a definition of one method agreeing with another on
@i{parameter specializers} and @i{qualifiers}, 
see @ref{Agreement on Parameter Specializers and Qualifiers}.

@item @t{*}  
If a @i{generic function} of the given name already exists
and if there is no @i{method object} that agrees with the new one on
@i{parameter specializers} and @i{qualifiers}, the existing @i{generic function}
@i{object} is modified to contain the new @i{method} @i{object}.

@item @t{*}  
If the given @i{name} names an @i{ordinary function}, a @i{macro},
or a @i{special operator}, an error is signaled.

@item @t{*}  
Otherwise a @i{generic function} is created with the @i{method}
specified by the @i{method-defining form}.

@end table

If the @i{lambda list} of a new @i{method} is not
@i{congruent} with the @i{lambda list} of the @i{generic function},
an error is signaled.  If a @i{method-defining operator} that cannot specify
@i{generic function} options creates a new @i{generic function}, 
a @i{lambda list} for that @i{generic function} is derived from the
@i{lambda list} of the @i{method} in the @i{method-defining form} in such a way
as to be @i{congruent} with it.  For a discussion of @i{congruence}
@IGindex congruence
,
see @ref{Congruent Lambda-lists for all Methods of a Generic Function}.

Each method has a @i{specialized lambda list}, which determines
when that method can be applied.  A @i{specialized lambda list} is like
an @i{ordinary lambda list} except that a specialized parameter
may occur instead of the name of a required parameter.  A specialized parameter
is a list @t{(@i{variable-name} @i{parameter-specializer-name})},
where @i{parameter-specializer-name} is one of the following:

@table @asis

@item a @i{symbol}  
denotes a @i{parameter specializer} which is the @i{class} 
named by that @i{symbol}.

@item a @i{class}  
denotes a @i{parameter specializer} which is the @i{class} itself.

@item @t{(eql @i{form})}  
denotes a @i{parameter specializer} which satisfies the @i{type specifier}
@t{(eql @i{object})}, where @i{object} is the 
result of evaluating @i{form}.  The form @i{form} is evaluated in 
the lexical environment in which the method-defining form is evaluated.
Note that @i{form} is evaluated only once, at the time the method is
defined, not each time the generic function is called.
@end table

@i{Parameter specializer names} are used in macros intended as the
user-level interface (@b{defmethod}), while @i{parameter specializers}
are used in the functional interface.

Only required parameters may be specialized, and there must be a
@i{parameter specializer} for each required parameter.  For notational
simplicity, if some required parameter in a @i{specialized lambda list} in
a method-defining form is simply a variable name, its 
@i{parameter specializer} defaults to the @i{class} @b{t}.

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 @i{parameter specializer}.

Let < A_1, ..., A_n> be the required
arguments to a generic function in order. Let < P_1,
..., P_n> be the @i{parameter specializers} corresponding to
the required parameters of the method M in order.  The method M is
applicable when each A_i is of the @i{type} specified by 
the @i{type specifier} P_i.
Because every valid @i{parameter specializer} is 
also a valid @i{type specifier}, the @i{function} @b{typep} can be used during method
selection to determine whether an argument satisfies a @i{parameter specializer}.  

A method all of whose @i{parameter specializers} are 
the @i{class} @b{t} is called a @i{default method}
@IGindex default method
; it is always applicable but
may be shadowed by a more specific method.

Methods can have @i{qualifiers}, which give the method combination
procedure a way to distinguish among methods.  A method that has one
or more @i{qualifiers} is called a @i{qualified method}.
A method with no @i{qualifiers} is called an @i{unqualified method}. 
A @i{qualifier} is any @i{non-list}.
The @i{qualifiers} defined by the @i{standardized} method combination types 
are @i{symbols}.

In this specification, the terms ``@i{primary method}'' and 
``@i{auxiliary method}'' are used to partition @i{methods}
within a method combination type according to their intended use.  
In standard method combination, @i{primary methods} are 
@i{unqualified methods} 
and @i{auxiliary methods} are methods with a single @i{qualifier} 
that is one of @t{:around}, @t{:before}, or @t{:after}.
@i{Methods} with these @i{qualifiers} are called @i{around methods},
@i{before methods}, and @i{after methods}, respectively.
When a method combination type is defined using the short form of
@b{define-method-combination}, @i{primary methods} are 
methods qualified with the name of the type of method combination, 
and auxiliary methods have the @i{qualifier} @t{:around}.
Thus the terms ``@i{primary method}'' and ``@i{auxiliary method}''
have only a relative definition within a given method combination type.

@node Agreement on Parameter Specializers and Qualifiers, Congruent Lambda-lists for all Methods of a Generic Function, Introduction to Methods, Generic Functions and Methods
@subsection Agreement on Parameter Specializers and Qualifiers

Two @i{methods} are said to agree with each other on @i{parameter specializers}
and @i{qualifiers} if the following conditions hold:

@table @asis

@item 1.  
Both methods have the same number of required parameters.
Suppose the @i{parameter specializers} of the two methods are
P_@{1,1@}... P_@{1,n@} and P_@{2,1@}... P_@{2,n@}.

@item 2.  
For each 1<= i<= n, P_@{1,i@} agrees with P_@{2,i@}.
The @i{parameter specializer} P_@{1,i@} agrees with P_@{2,i@} if
P_@{1,i@} and P_@{2,i@} are the same class or if 
P_@{1,i@}=@t{(@b{eql} @i{object}_1)},
P_@{2,i@}=@t{(@b{eql} @i{object}_2)}, and
@t{(@b{eql} @i{object}_1 @i{object}_2)}.
Otherwise P_@{1,i@} and P_@{2,i@} do not agree.

@item 3.  
The two @i{lists} of @i{qualifiers} are the @i{same} 
under @b{equal}.

@end table

@node Congruent Lambda-lists for all Methods of a Generic Function, Keyword Arguments in Generic Functions and Methods, Agreement on Parameter Specializers and Qualifiers, Generic Functions and Methods
@subsection Congruent Lambda-lists for all Methods of a Generic Function

These rules define the congruence of a set of @i{lambda lists}, including the
@i{lambda list} of each method for a given generic function and the
@i{lambda list} specified for the generic function itself, if given.

@table @asis

@item 1.  
Each @i{lambda list} must have the same number of required
parameters.

@item 2.  
Each @i{lambda list} must have the same number of optional
parameters.  Each method can supply its own default for an optional
parameter.

@item 3.  
If any @i{lambda list} mentions @b{&rest} or @b{&key}, each
@i{lambda list} must mention one or both of them.

@item 4.  
If the @i{generic function} @i{lambda list}
mentions @b{&key}, each
method must accept all of the keyword names mentioned after @b{&key},
either by accepting them explicitly, by specifying @b{&allow-other-keys},
or by specifying @b{&rest} but not @b{&key}.
Each method can accept additional keyword arguments of its own.  The
checking of the validity of keyword names is done in the generic
function, not in each method.
A method is invoked as if the keyword
argument pair whose name is @t{:allow-other-keys} and whose value
is @i{true} were supplied, though no such argument pair will be passed.

@item 5.  
The use of @b{&allow-other-keys} need not be consistent
across @i{lambda lists}.  If @b{&allow-other-keys} is mentioned in 
the @i{lambda list} of any applicable @i{method} or of the @i{generic function},
any keyword arguments may be mentioned in the call to the @i{generic function}.

@item 6.  
The use of @b{&aux} need not be consistent across methods.

If a @i{method-defining operator} that cannot specify @i{generic function} options
creates a @i{generic function}, and if the @i{lambda list} for the method
mentions keyword arguments, the @i{lambda list} of the generic function
will mention @b{&key} (but no keyword arguments).

@end table

@node Keyword Arguments in Generic Functions and Methods, Method Selection and Combination, Congruent Lambda-lists for all Methods of a Generic Function, Generic Functions and Methods
@subsection Keyword Arguments in Generic Functions and Methods

When a generic function or any of its methods mentions 
@b{&key} in a @i{lambda list}, the specific set of keyword
arguments accepted by the generic function varies according to the
applicable methods.  The set of keyword arguments accepted by the
generic function for a particular call is the union of the keyword
arguments accepted by all applicable methods and the keyword arguments
mentioned after @b{&key} in the generic function definition,
if any.  A method that has @b{&rest} but not @b{&key} does not affect the
set of acceptable keyword arguments.  If
the @i{lambda list} of any applicable method or of the generic
function definition contains @b{&allow-other-keys}, all
keyword arguments are accepted by the generic function.

The @i{lambda list} congruence rules require that each method
accept all of the keyword arguments mentioned after @b{&key} in the
generic function definition, by accepting them explicitly, by
specifying @b{&allow-other-keys}, or by specifying @b{&rest} but
not @b{&key}.  Each method can accept additional keyword arguments
of its own, in addition to the keyword arguments mentioned in the
generic function definition.

If a @i{generic function} is passed a keyword argument that no applicable
method accepts, an error should be signaled; see @ref{Error Checking in Function Calls}.

@menu
* Examples of Keyword Arguments in Generic Functions and Methods::  
@end menu

@node Examples of Keyword Arguments in Generic Functions and Methods,  , Keyword Arguments in Generic Functions and Methods, Keyword Arguments in Generic Functions and Methods
@subsubsection Examples of Keyword Arguments in Generic Functions and Methods

For example, suppose there are two methods defined for @t{width}
as follows:

@example
 (defmethod width ((c character-class) &key font) ...)

 (defmethod width ((p picture-class) &key pixel-size) ...)
@end example

@noindent
Assume that there are no other methods and no generic
function definition for @t{width}. The evaluation of the
following form should signal an error because 
the keyword argument @t{:pixel-size} is not accepted by the applicable method.

@example
 (width (make-instance `character-class :char #\Q) 
        :font 'baskerville :pixel-size 10)
@end example

The evaluation of the following form should signal an error.

@example
 (width (make-instance `picture-class :glyph (glyph #\Q)) 
        :font 'baskerville :pixel-size 10)
@end example

The evaluation of the following form will not signal an error
if the class named @t{character-picture-class} is a subclass of
both @t{picture-class} and @t{character-class}.

@example
 (width (make-instance `character-picture-class :char #\Q)
        :font 'baskerville :pixel-size 10)
@end example

@node Method Selection and Combination, Inheritance of Methods, Keyword Arguments in Generic Functions and Methods, Generic Functions and Methods
@subsection Method Selection and Combination

When a @i{generic function} is called with particular arguments, it must
determine the code to execute.  This code is called the 
@i{effective method}
@IGindex effective method
 for those @i{arguments}.
The @i{effective method} is a 
combination of the @i{applicable methods} in the @i{generic function}
that @i{calls} some or all of the @i{methods}.

If a @i{generic function} is called and no @i{methods} are 
@i{applicable}, the @i{generic function} @b{no-applicable-method}
is invoked, with the @i{results} from that call being used as the
@i{results} of the call to the original @i{generic function}.  Calling
@b{no-applicable-method} takes precedence over checking for acceptable
keyword arguments; see @ref{Keyword Arguments in Generic Functions and Methods}.

When the @i{effective method} has been determined,
it is invoked with the same @i{arguments} as were passed to the @i{generic function}.  
Whatever @i{values} it returns are returned as the @i{values}
of the @i{generic function}.

@menu
* Determining the Effective Method::  
* Selecting the Applicable Methods::  
* Sorting the Applicable Methods by Precedence Order::	
* Applying method combination to the sorted list of applicable methods::  
* Standard Method Combination::	 
* Declarative Method Combination::  
* Built-in Method Combination Types::  
@end menu

@node Determining the Effective Method, Selecting the Applicable Methods, Method Selection and Combination, Method Selection and Combination
@subsubsection Determining the Effective Method

The effective method is determined by the following three-step procedure:

@table @asis

@item 1.  
@r{Select the applicable methods.}

@item 2.  
@r{Sort the applicable methods by precedence order, putting
the most specific method first.}

@item 3.  
@r{Apply method combination to the sorted list of
applicable methods, producing the effective method.}

@end table

@node Selecting the Applicable Methods, Sorting the Applicable Methods by Precedence Order, Determining the Effective Method, Method Selection and Combination
@subsubsection Selecting the Applicable Methods

This step is described in @ref{Introduction to Methods}.

@node Sorting the Applicable Methods by Precedence Order, Applying method combination to the sorted list of applicable methods, Selecting the Applicable Methods, Method Selection and Combination
@subsubsection Sorting the Applicable Methods by Precedence Order

To compare the precedence of two methods, their @i{parameter specializers}
are examined in order.  The default examination order is from left to
right, but an alternative order may be specified by the 
@t{:argument-precedence-order} option to @b{defgeneric} or to any of
the other operators that specify generic function options.

The corresponding @i{parameter specializers} from each method are
compared.  When a pair of @i{parameter specializers} agree, the next
pair are compared for agreement.  If all corresponding parameter
specializers agree, the two methods must have different
@i{qualifiers}; in this case, either method can be selected to precede the
other.  For information about agreement, see @ref{Agreement on Parameter Specializers and Qualifiers}.

If some corresponding @i{parameter specializers} do not agree, the first
pair of @i{parameter specializers} that do not agree determines the
precedence.  If both @i{parameter specializers} are classes, the more
specific of the two methods is the method whose @i{parameter specializer}
appears earlier in the @i{class precedence list} of the corresponding
argument.  Because of the way in which the set of applicable methods
is chosen, the @i{parameter specializers} are guaranteed to be present in
the class precedence list of the class of the argument.

If just one of a pair of corresponding @i{parameter specializers} is @t{(eql @i{object})},
the @i{method} with that @i{parameter specializer} precedes the
other @i{method}.  If both @i{parameter specializers} are @b{eql}
@i{expressions}, the
specializers must agree (otherwise the two @i{methods} would
not both have been applicable to this argument).

The resulting list of @i{applicable methods} has the most specific
@i{method} first and the least specific @i{method} last.    

@node Applying method combination to the sorted list of applicable methods, Standard Method Combination, Sorting the Applicable Methods by Precedence Order, Method Selection and Combination
@subsubsection Applying method combination to the sorted list of applicable methods

In the simple case---if standard method combination is used and all
applicable methods are primary methods---the 
effective method is the most specific method.
That method can call the next most specific
method by using the @i{function} @b{call-next-method}.  The method that
@b{call-next-method} will call is referred to as the 
@i{next method}
@IGindex next method
.  The predicate @b{next-method-p} tests whether a next
method exists.  If @b{call-next-method} is called and there is no
next most specific method, the generic function @b{no-next-method}
is invoked.

In general, the effective method is some combination of the applicable
methods.  It is described by a @i{form} that contains calls to some or
all of the applicable methods, returns the value or values that will
be returned as the value or values of the generic function, and
optionally makes some of the methods accessible by means of 
@b{call-next-method}.

The role of each method in the effective method is determined by its
@i{qualifiers} and the specificity of the method.  A @i{qualifier}
serves to mark a method, and the meaning of a @i{qualifier} is
determined by the way that these marks are used by this step
of the procedure.  If an applicable method has an unrecognized
@i{qualifier}, this step signals an error and does not include that method
in the effective method.

When standard method combination is used together with qualified methods, 
the effective method is produced as described in @ref{Standard Method Combination}.

Another type of method combination can be specified by using the
@t{:method-combination} option of @b{defgeneric} or
of any of the other operators that specify generic function options.  In
this way this step of the procedure can be customized.

New types of method combination can be defined by using 
the @b{define-method-combination} @i{macro}. 

@node Standard Method Combination, Declarative Method Combination, Applying method combination to the sorted list of applicable methods, Method Selection and Combination
@subsubsection Standard Method Combination

@IRindex standard

Standard method combination is supported by the @i{class} @b{standard-generic-function}.
It is used if no other type of method
combination is specified or if the built-in method combination type
@b{standard} is specified. 

Primary methods define the main action of the effective method,  
while auxiliary methods modify that action in one of three ways.
A primary method has no method @i{qualifiers}.

An auxiliary method is a method whose 
@i{qualifier} is @t{:before}, @t{:after}, or @t{:around}.
Standard method combination
allows no more than one @i{qualifier} per method; if a method definition
specifies more than one @i{qualifier} per method, an error is signaled.

@table @asis

@item @t{*}  
A @i{before method} has the keyword @t{:before} as its only @i{qualifier}.
A @i{before method} specifies @i{code} that is to be run before any 
@i{primary methods}.

@item @t{*}  
An @i{after method} has the keyword @t{:after} as its only @i{qualifier}.
An @i{after method} specifies @i{code} that is to be run after
@i{primary methods}.

@item @t{*}  
An @i{around method} has the keyword @t{:around} as its only @i{qualifier}.
An @i{around method} specifies @i{code} that is to be run instead of other
@i{applicable methods},
but which might contain explicit @i{code}
which calls some of those @i{shadowed} @i{methods}
(via @b{call-next-method}).

@end table

The semantics of standard method combination is as follows:

@table @asis

@item @t{*}  
If there are any @i{around methods}, the most specific
@i{around method} is called.  It supplies the value or values of the
generic function.

@item @t{*}  
Inside the body of an @i{around method}, 
@b{call-next-method} can be used to call the @i{next method}.  When the next
method returns, the @i{around method} can execute more code,
perhaps based on the returned value or values.
The @i{generic function} @b{no-next-method} is invoked if @b{call-next-method} is used and
there is no @i{applicable method} to call.  The @i{function} @b{next-method-p}
may be used to determine whether a @i{next method} exists.

@item @t{*}  
If an @i{around method} invokes @b{call-next-method},
the next most specific @i{around method}
is called, if one is applicable.  If there are no @i{around methods} 
or if @b{call-next-method} is called by the least
specific @i{around method}, the other methods are called as
follows:
@table @asis

@item --  
All the @i{before methods} are called, in
most-specific-first order.  Their values are ignored.
An error is signaled if @b{call-next-method} is used in a
@i{before method}.

@item --  
The most specific primary method is called.  Inside the
body of a primary method, @b{call-next-method} may be used to call
the next most specific primary method.  When that method returns, the
previous primary method can execute more code, perhaps based on the
returned value or values.  The generic function @b{no-next-method}
is invoked if @b{call-next-method} is used and there are no more
applicable primary methods.  The @i{function} @b{next-method-p} may be
used to determine whether a @i{next method} exists.  If @b{call-next-method}
is not used, only the most specific @i{primary method} is called.

@item --  
All the @i{after methods} are called in
most-specific-last order.  Their values are ignored.
An error is signaled if @b{call-next-method} is used in an
@i{after method}.
@end table

@item @t{*}  
If no @i{around methods} were invoked, the most
specific primary method supplies the value or values returned by the
generic function.  The value or values returned by the invocation of
@b{call-next-method} in the least specific @i{around method} are
those returned by the most specific primary method.

@end table

In standard method combination, if there is an applicable method
but no applicable primary method, an error is signaled.

The @i{before methods} are run in most-specific-first order while
the @i{after methods} are run in least-specific-first order.  The
design rationale for this difference can be illustrated with an
example.  Suppose class C_1 modifies the behavior of its
superclass, C_2, by adding @i{before methods} and @i{after methods}.
Whether the behavior of the class C_2 is defined
directly by methods on C_2 or is inherited from its superclasses
does not affect the relative order of invocation of methods on
instances of the class C_1.  Class C_1's 
@i{before method} runs before all of class C_2's methods.  
Class C_1's @i{after method} runs after all of class C_2's methods.

By contrast, all @i{around methods} run before any other methods
run.  Thus a less specific @i{around method} runs before a more
specific primary method.

If only primary methods are used and if @b{call-next-method} is not
used, only the most specific method is invoked; that is, more specific
methods shadow more general ones. 

@node Declarative Method Combination, Built-in Method Combination Types, Standard Method Combination, Method Selection and Combination
@subsubsection Declarative Method Combination

The macro @b{define-method-combination} defines new forms of method
combination.  It provides a mechanism for customizing the production
of the effective method. The default procedure for producing an
effective method is described in @ref{Determining the Effective Method}.
There are two forms of
@b{define-method-combination}.  The short form is a simple facility while
the long form is more powerful and more verbose.  The long form
resembles @b{defmacro} in that the body is an expression that
computes a Lisp form; it provides mechanisms for implementing
arbitrary control structures within method combination and for
arbitrary processing of method @i{qualifiers}.  

@node Built-in Method Combination Types,  , Declarative Method Combination, Method Selection and Combination
@subsubsection Built-in Method Combination Types

The object system provides a set of built-in method combination types.  To
specify that a generic function is to use one of these method
combination types, the name of the method combination type is given as
the argument to the @t{:method-combination} option to 
@b{defgeneric} or to the @t{:method-combination} option to any of the
other operators that specify generic function options.

The names of the built-in  method combination types are listed in Figure 7--3.

@IRindex +

@IRindex and

@IRindex append

@IRindex list

@IRindex max

@IRindex min

@IRindex nconc

@IRindex or

@IRindex progn

@IRindex standard

@format
@group
@noindent
@w{  +    append  max  nconc  progn     }
@w{  and  list    min  or     standard  }

@noindent
@w{  Figure 7--3: Built-in Method Combination Types}

@end group
@end format

The semantics of the @b{standard} built-in method combination type is
described in @ref{Standard Method Combination}.  The other
built-in method combination types are called simple built-in method
combination types.

The simple built-in method combination types act as though they were
defined by the short form of @b{define-method-combination}.  
They recognize two roles for @i{methods}:

@table @asis

@item @t{*}  
An @i{around method} has the keyword symbol 
@t{:around} as its sole @i{qualifier}.  The meaning of 
@t{:around} @i{methods} is the same as in standard method combination.
Use of the functions @b{call-next-method} and @b{next-method-p}
is supported in @i{around methods}.

@item @t{*}  
A primary method has the name of the method combination
type as its sole @i{qualifier}.  For example, the built-in method
combination type @t{and} recognizes methods whose sole @i{qualifier} is
@t{and}; these are primary methods. Use of the functions 
@b{call-next-method} and @b{next-method-p} is not supported 
in @i{primary methods}.

@end table

The semantics of the simple built-in method combination types is as
follows:

@table @asis

@item @t{*}  
If there are any @i{around methods}, the most specific @i{around method}
is called.   It supplies the value or values of the @i{generic function}. 

@item @t{*}  
Inside the body of an @i{around method}, the function
@b{call-next-method} can be used to call the @i{next method}.
The @i{generic function} @b{no-next-method} is invoked if 
@b{call-next-method} is used and there is no applicable method to call.
The @i{function} @b{next-method-p} may be used to determine whether a
@i{next method} exists. When the @i{next method} returns, 
the @i{around method} can execute more code,
perhaps based on the returned value or values.

@item @t{*}  
If an @i{around method} invokes @b{call-next-method},
the next most specific @i{around method} is
called, if one is applicable.  If there are no @i{around methods}
or if @b{call-next-method} is called by the least specific
@i{around method}, a Lisp form derived from the name of the built-in
method combination type and from the list of applicable primary
methods is evaluated to produce the value of the generic function.
Suppose the name of the method combination type is @i{operator}
and the call to the generic function is of the form

@center (@i{generic-function} a_1... a_n)

@item @t{}  
Let M_1,...,M_k be the applicable primary methods
in order; then the derived Lisp form is

@center (@i{operator} < M_1
 a_1... a_n>...<
M_k a_1... a_n>)

@item @t{}  
If the expression < M_i  a_1... a_n> is
evaluated, the method M_i will be applied to the arguments
a_1... a_n.  
For example,
if @i{operator} is @t{or},
the expression < M_i  a_1... a_n> is
evaluated only if < M_j  a_1... a_n>,
1<= j<i, returned @t{nil}.

@item @t{}  
The default order for the primary methods is 
@t{:most-specific-first}.  However, the order can be reversed by supplying
@t{:most-specific-last} as the second argument to the @t{:method-combination} option.
@end table

The simple built-in method combination types require exactly one
@i{qualifier} per method.  An error is signaled if there are applicable
methods with no @i{qualifiers} or with @i{qualifiers} that are not supported
by the method combination type. An error is signaled if there are
applicable @i{around methods} and no applicable primary
methods.

@node Inheritance of Methods,  , Method Selection and Combination, Generic Functions and Methods
@subsection Inheritance of Methods

A subclass inherits methods in the sense that any method applicable to
all instances of a class is also applicable to all instances of any
subclass of that class.

The inheritance of methods acts the same way regardless of 
which of the @i{method-defining operators} created the methods.

The inheritance of methods is described in detail in 
@ref{Method Selection and Combination}.

@c end of including concept-gfs-and-methods

@node Objects Dictionary,  , Generic Functions and Methods, Objects
@section Objects Dictionary

@c including dict-objects

@menu
* function-keywords::		
* ensure-generic-function::	
* allocate-instance::		
* reinitialize-instance::	
* shared-initialize::		
* update-instance-for-different-class::	 
* update-instance-for-redefined-class::	 
* change-class::		
* slot-boundp::			
* slot-exists-p::		
* slot-makunbound::		
* slot-missing::		
* slot-unbound::		
* slot-value::			
* method-qualifiers::		
* no-applicable-method::	
* no-next-method::		
* remove-method::		
* make-instance::		
* make-instances-obsolete::	
* make-load-form::		
* make-load-form-saving-slots::	 
* with-accessors::		
* with-slots::			
* defclass::			
* defgeneric::			
* defmethod::			
* find-class::			
* next-method-p::		
* call-method::			
* call-next-method::		
* compute-applicable-methods::	
* define-method-combination::	
* find-method::			
* add-method::			
* initialize-instance::		
* class-name::			
* setf class-name::		
* class-of::			
* unbound-slot::		
* unbound-slot-instance::	
@end menu

@node function-keywords, ensure-generic-function, Objects Dictionary, Objects Dictionary
@subsection function-keywords                               [Standard Generic Function]

@subsubheading  Syntax::

@code{function-keywords}  @i{method} @result{}  @i{keys, allow-other-keys-p}

@subsubheading  Method Signatures::

@code{function-keywords}  @i{@r{(}@i{method} @b{standard-method}@r{)}}

@subsubheading  Arguments and Values::

@i{method}---a @i{method}.

@i{keys}---a @i{list}.

@i{allow-other-keys-p}---a @i{generalized boolean}.

@subsubheading  Description::

Returns the keyword parameter specifiers for a @i{method}.

Two values are returned: 
 a @i{list} of the explicitly named keywords 
 and a @i{generalized boolean} that states whether @b{&allow-other-keys}
  had been specified in the @i{method} definition.

@subsubheading  Examples::

@example
 (defmethod gf1 ((a integer) &optional (b 2)
                 &key (c 3) ((:dee d) 4) e ((eff f)))
   (list a b c d e f))
@result{}  #<STANDARD-METHOD GF1 (INTEGER) 36324653>
 (find-method #'gf1 '() (list (find-class 'integer))) 
@result{}  #<STANDARD-METHOD GF1 (INTEGER) 36324653>
 (function-keywords *)
@result{}  (:C :DEE :E EFF), @i{false}
 (defmethod gf2 ((a integer))
   (list a b c d e f))
@result{}  #<STANDARD-METHOD GF2 (INTEGER) 42701775>
 (function-keywords (find-method #'gf1 '() (list (find-class 'integer))))
@result{}  (), @i{false}
 (defmethod gf3 ((a integer) &key b c d &allow-other-keys)
   (list a b c d e f))
 (function-keywords *)
@result{}  (:B :C :D), @i{true}
@end example

@subsubheading  Affected By::

@b{defmethod}

@subsubheading  See Also::

@ref{defmethod}

@node ensure-generic-function, allocate-instance, function-keywords, Objects Dictionary
@subsection ensure-generic-function                                          [Function]

@code{ensure-generic-function}  @i{function-name @r{&key}
			 argument-precedence-order declare
                               documentation environment
                               generic-function-class lambda-list
                               method-class method-combination}@*
   @result{}  @i{generic-function}

@subsubheading  Arguments and Values::

@i{function-name}---a @i{function name}.

The keyword arguments correspond to the @i{option} arguments of
@b{defgeneric}, except that the @t{:method-class} and
@t{:generic-function-class} arguments can be @i{class} @i{object}s
as well as names.

@t{Method-combination} -- method combination object.

@t{Environment} -- the same as the @b{&environment} argument
to macro expansion functions and is used to distinguish between compile-time
and run-time environments.

[Editorial Note by KMP: What about documentation. Missing from this arguments enumeration,
  and confusing in description below.]

@i{generic-function}---a @i{generic function} @i{object}.

@subsubheading  Description::

The @i{function} @b{ensure-generic-function} is used to define 
a globally named @i{generic function} with no @i{methods} 
or to specify or modify options and declarations that pertain to 
a globally named @i{generic function} as a whole.

If @i{function-name} is not @i{fbound} in the @i{global environment},
a new
@i{generic function} is created.  
If 

@t{(fdefinition @i{function-name})} 

is an @i{ordinary function}, 
a @i{macro}, 
or a @i{special operator},
an error is signaled.

If @i{function-name} 
is a @i{list}, it must be of the
form @t{(setf @i{symbol})}.
If @i{function-name} specifies a @i{generic function} that has a
different value for any of the following arguments,
the @i{generic function} is modified to have the new value: 
@t{:argument-precedence-order}, @t{:declare}, @t{:documentation},
@t{:method-combination}.

If @i{function-name} specifies a @i{generic function} that has a
different value for the @t{:lambda-list} argument, and the new value
is congruent with the @i{lambda lists} of all existing 
@i{methods} or there
are no @i{methods}, the value is changed; otherwise an error is signaled.

If @i{function-name} specifies a @i{generic function} that has a
different value for the @t{:generic-function-class} argument and if
the new generic function class is compatible with the old,
@b{change-class} is called to change the @i{class} of the 
@i{generic function};
otherwise an error is signaled.

If @i{function-name} specifies a @i{generic function} that has a
different value for the @t{:method-class} argument, the value is
changed, but any existing @i{methods} are not changed.

@subsubheading  Affected By::

Existing function binding of @i{function-name}.

@subsubheading  Exceptional Situations::

If 

@t{(fdefinition @i{function-name})}

is an @i{ordinary function}, a @i{macro}, or a @i{special operator}, 
an error of @i{type} @b{error} is signaled.

If @i{function-name} specifies a 
@i{generic function} that has a
different value for the @t{:lambda-list} argument, and the new value
is not congruent with the @i{lambda list} of any existing 
@i{method},
an error of @i{type} @b{error} is signaled.

If @i{function-name} specifies a 
@i{generic function} that has a
different value for the @t{:generic-function-class} argument and if
the new generic function class not is compatible with the old,
an error of @i{type} @b{error} is signaled.

@subsubheading  See Also::

@ref{defgeneric}

@node allocate-instance, reinitialize-instance, ensure-generic-function, Objects Dictionary
@subsection allocate-instance                               [Standard Generic Function]

@subsubheading  Syntax::

@code{allocate-instance}  @i{class @r{&rest} initargs @r{&key} @r{&allow-other-keys}} @result{}  @i{new-instance}

@subsubheading  Method Signatures::

@code{allocate-instance}  @i{@r{(}@i{class} @b{standard-class}@r{)} @r{&rest} initargs}

@code{allocate-instance}  @i{@r{(}@i{class} @b{structure-class}@r{)} @r{&rest} initargs}

@subsubheading  Arguments and Values::

@i{class}---a @i{class}.

@i{initargs}---a @i{list} of @i{keyword/value pairs} 
		   (initialization argument @i{names} and @i{values}).

@i{new-instance}---an @i{object} whose @i{class} is @i{class}.

@subsubheading  Description::

The generic function @b{allocate-instance} creates and returns
a new instance of the @i{class}, without initializing it.
When the @i{class} is a @i{standard class}, this means that
the @i{slots} are @i{unbound}; when the @i{class} is a
@i{structure class}, this means the @i{slots}' @i{values}
are unspecified.

The caller of @b{allocate-instance} is expected to have
already checked the initialization arguments.

The @i{generic function} @b{allocate-instance} is called by
@b{make-instance}, as described in
@ref{Object Creation and Initialization}.

@subsubheading  See Also::

@ref{defclass}
, 
@ref{make-instance}
, 
@ref{class-of}
,
@ref{Object Creation and Initialization}

@subsubheading  Notes::

The consequences of adding @i{methods} to @b{allocate-instance} is unspecified.
This capability might be added by the @i{Metaobject Protocol}.

@node reinitialize-instance, shared-initialize, allocate-instance, Objects Dictionary
@subsection reinitialize-instance                           [Standard Generic Function]

@subsubheading  Syntax::

@code{reinitialize-instance}  @i{instance @r{&rest} initargs @r{&key} @r{&allow-other-keys}} @result{}  @i{instance}

@subsubheading  Method Signatures::

@code{reinitialize-instance}  @i{@r{(}@i{instance} @b{standard-object}@r{)} @r{&rest} initargs}

@subsubheading  Arguments and Values::

@i{instance}---an @i{object}.

@i{initargs}---an @i{initialization argument list}.

@subsubheading  Description::

The @i{generic function} @b{reinitialize-instance} can be used to change
the values of @i{local slots} of an @i{instance} according to 
@i{initargs}.
This @i{generic function} can be called by users.

The system-supplied primary @i{method} for @b{reinitialize-instance}
checks the validity of @i{initargs} and signals an error if
an @i{initarg} is supplied that is not declared as valid.
The @i{method} then calls the generic function @b{shared-initialize}
with the following arguments:  the @i{instance}, 
@b{nil} (which means no @i{slots}
should be initialized according to their initforms), and the
@i{initargs} it received.

@subsubheading  Side Effects::

The @i{generic function} @b{reinitialize-instance} changes the values of @i{local slots}.

@subsubheading  Exceptional Situations::

The system-supplied primary @i{method} for @b{reinitialize-instance}
signals an error if an @i{initarg} is supplied that is not declared as valid.

@subsubheading  See Also::

@ref{Initialize-Instance}
,
@ref{Shared-Initialize}
,
@ref{update-instance-for-redefined-class}
,
@ref{update-instance-for-different-class}
,
@ref{slot-boundp}
,
@ref{slot-makunbound}
,
@ref{Reinitializing an Instance},
@ref{Rules for Initialization Arguments},
@ref{Declaring the Validity of Initialization Arguments}

@subsubheading  Notes::

@i{Initargs} are declared as valid by using the
@t{:initarg} option to @b{defclass}, or by defining 
@i{methods} for @b{reinitialize-instance}
or @b{shared-initialize}.  The keyword name
of each keyword parameter specifier in the @i{lambda list} of any 
@i{method}
defined on @b{reinitialize-instance} or @b{shared-initialize} is
declared as a valid initialization argument name for all 
@i{classes} for
which that @i{method} is applicable.

@node shared-initialize, update-instance-for-different-class, reinitialize-instance, Objects Dictionary
@subsection shared-initialize                               [Standard Generic Function]

@subsubheading  Syntax::

@code{shared-initialize}  @i{instance slot-names @r{&rest} initargs @r{&key} @r{&allow-other-keys}} @result{}  @i{instance}

@subsubheading  Method Signatures::

@code{shared-initialize}  @i{@r{(}@i{instance} @b{standard-object}@r{)} slot-names @r{&rest} initargs}

@subsubheading  Arguments and Values::

@i{instance}---an @i{object}.

@i{slot-names}---a @i{list} or @b{t}.

@i{initargs}---a @i{list} of @i{keyword/value pairs}
		   (of initialization argument @i{names} and @i{values}).

@subsubheading  Description::

The generic function @b{shared-initialize} is used to fill the 
@i{slots}                        
of an @i{instance} 
using @i{initargs} and @t{:initform}
forms.  It is called when an instance is created, when an instance is
re-initialized, when an instance is updated to conform to a redefined
@i{class}, and when an instance is updated to conform to a different
@i{class}. The generic function @b{shared-initialize} is called by the
system-supplied primary @i{method} for @b{initialize-instance},
@b{reinitialize-instance}, @b{update-instance-for-redefined-class}, and
@b{update-instance-for-different-class}.

The generic function @b{shared-initialize} takes the following
arguments: the @i{instance} to be initialized, a specification of a set of
@i{slot-names} @i{accessible} in that @i{instance}, 
and any number of @i{initargs}.
The arguments after the first two must form an 
@i{initialization argument list}.  The system-supplied primary @i{method} on 
@b{shared-initialize} initializes the @i{slots} with values according to the
@i{initargs} and supplied @t{:initform} forms.  @i{Slot-names}
indicates which @i{slots} should be initialized according
to their @t{:initform} forms if no @i{initargs} are
provided for those @i{slots}. 

The system-supplied primary @i{method} behaves as follows, 
regardless of whether the @i{slots} are local or shared: 

@table @asis

@item @t{*}  
If an @i{initarg} in the @i{initialization argument list} 
 specifies a value for that @i{slot}, that
 value is stored into the @i{slot}, even if a value has
 already been stored in the @i{slot} before the @i{method} is run.

@item @t{*}  
Any @i{slots} indicated by @i{slot-names} that are still unbound
 at this point are initialized according to their @t{:initform} forms.
 For any such @i{slot} that has an @t{:initform} form,
 that @i{form} is evaluated in the lexical environment of its defining 
 @b{defclass} @i{form} and the result is stored into the @i{slot}.
 For example, if a @i{before method} stores a value in the @i{slot}, 
 the @t{:initform} form will not be used to supply a value for the @i{slot}.

@item @t{*}  
The rules mentioned in @ref{Rules for Initialization Arguments} are obeyed.

@end table

The @i{slots-names} argument specifies the @i{slots} that are to be
initialized according to their @t{:initform} forms if no
initialization arguments apply.  It can be a @i{list} of slot @i{names}, 
which specifies the set of those slot @i{names}; or it can be the @i{symbol} @b{t}, 
which specifies the set of all of the @i{slots}.

@subsubheading  See Also::

@ref{Initialize-Instance}
,
@ref{reinitialize-instance}
,
@ref{update-instance-for-redefined-class}
,
@ref{update-instance-for-different-class}
,
@ref{slot-boundp}
,
@ref{slot-makunbound}
,
@ref{Object Creation and Initialization},
@ref{Rules for Initialization Arguments},
@ref{Declaring the Validity of Initialization Arguments}

@subsubheading  Notes::

@i{Initargs} are declared as valid by using the @t{:initarg}
option to @b{defclass}, or by defining 
@i{methods} for @b{shared-initialize}. 
The keyword name of each keyword parameter
specifier in the @i{lambda list} of any @i{method} defined on 
@b{shared-initialize} is declared as a valid @i{initarg}
name for all @i{classes} for which that @i{method} is applicable.

Implementations are permitted to optimize @t{:initform} forms that 
neither produce nor depend on side effects, by evaluating these @i{forms}
and storing them into slots before running any 
@b{initialize-instance} methods, rather than by handling them in the
primary @b{initialize-instance} method.  (This optimization might
be implemented by having the @b{allocate-instance} method copy a
prototype instance.)

Implementations are permitted to optimize default initial value forms
for @i{initargs} associated with slots by not actually
creating the complete initialization argument 
@i{list} when the only @i{method}
that would receive the complete @i{list} is the 
@i{method} on @b{standard-object}.
In this case default initial value forms can be 
treated like @t{:initform} forms.  This optimization has no visible
effects other than a performance improvement.

@node update-instance-for-different-class, update-instance-for-redefined-class, shared-initialize, Objects Dictionary
@subsection update-instance-for-different-class             [Standard Generic Function]

@subsubheading  Syntax::

@code{update-instance-for-different-class}  @i{previous current 
		   @r{&rest} initargs
		   @r{&key} @r{&allow-other-keys}} @result{}  @i{@i{implementation-dependent}}

@subsubheading  Method Signatures::

@code{update-instance-for-different-class}  @i{@r{(}@i{previous} @b{standard-object}@r{)}
	        @r{(}@i{current} @b{standard-object}@r{)}
	        @r{&rest} initargs}

@subsubheading  Arguments and Values::

@i{previous}---a copy of the original @i{instance}.

@i{current}---the original @i{instance} (altered).

@i{initargs}---an @i{initialization argument list}.

@subsubheading  Description::

The generic function @b{update-instance-for-different-class} is not
intended to be called by programmers.  Programmers may write
@i{methods} for it.  The @i{function} @b{update-instance-for-different-class}
is called only by the @i{function} @b{change-class}.

The system-supplied primary @i{method} on 
@b{update-instance-for-different-class} checks the validity of
@i{initargs} and signals an error if an @i{initarg}
is supplied that is not declared as valid.  This @i{method} then
initializes @i{slots} with values according to the @i{initargs},
and initializes the newly added @i{slots} with values according
to their @t{:initform} forms.  It does this by calling the generic
function @b{shared-initialize} with the following arguments: the 
instance (@i{current}),
a list of @i{names} of the newly added @i{slots}, and the @i{initargs}
it received.  Newly added @i{slots} are those @i{local slots} for which
no @i{slot} of the same name exists in the @i{previous} class.

@i{Methods} for @b{update-instance-for-different-class} can be defined to
specify actions to be taken when an @i{instance} is updated.  If only 
@i{after methods} for @b{update-instance-for-different-class} are
defined, they will be run after the system-supplied primary @i{method} for
initialization and therefore will not interfere with the default
behavior of @b{update-instance-for-different-class}.

@i{Methods} on @b{update-instance-for-different-class} can be defined to
initialize @i{slots} differently from @b{change-class}.  The default
behavior of @b{change-class} is described in 
@ref{Changing the Class of an Instance}.

The arguments to @b{update-instance-for-different-class} are
computed by @b{change-class}.  When @b{change-class} is invoked on
an @i{instance}, a copy of that @i{instance} is made; @b{change-class} then
destructively alters the original @i{instance}. The first argument to
@b{update-instance-for-different-class}, @i{previous}, is that
copy; it holds the old @i{slot} values temporarily.  This argument has
dynamic extent within @b{change-class}; if it is referenced in any
way once @b{update-instance-for-different-class} returns, the
results are undefined.  The second argument to
@b{update-instance-for-different-class}, @i{current}, is the altered
original @i{instance}.
The intended use of @i{previous} is to extract old @i{slot} values by using
@b{slot-value} or @b{with-slots} or by invoking 
a reader generic function, or to run other @i{methods} that were applicable to 
@i{instances} of
the original @i{class}.

@subsubheading  Examples::

See the example for the @i{function} @b{change-class}.

@subsubheading  Exceptional Situations::
The system-supplied primary @i{method} on
@b{update-instance-for-different-class} signals an error if an
initialization argument is supplied that is not declared as valid.

@subsubheading  See Also::

@ref{change-class}
,
@ref{Shared-Initialize}
,
@ref{Changing the Class of an Instance},
@ref{Rules for Initialization Arguments},
@ref{Declaring the Validity of Initialization Arguments}

@subsubheading  Notes::

@i{Initargs} are declared as valid by using the @t{:initarg}
option to @b{defclass}, or by defining @i{methods}
for @b{update-instance-for-different-class} or @b{shared-initialize}.
The keyword name of each keyword parameter specifier in the @i{lambda list} of
any @i{method} defined on @b{update-instance-for-different-class}
or @b{shared-initialize} is declared as a valid @i{initarg} name
for all @i{classes} for which that @i{method} is applicable.

The value returned by @b{update-instance-for-different-class} is
ignored by @b{change-class}.

@node update-instance-for-redefined-class, change-class, update-instance-for-different-class, Objects Dictionary
@subsection update-instance-for-redefined-class             [Standard Generic Function]

@subsubheading  Syntax::

@code{update-instance-for-redefined-class}  @i{instance
                                added-slots discarded-slots
                                property-list
                                @r{&rest} initargs @r{&key} @r{&allow-other-keys}}@*
   @result{}  @i{@{@i{result}@}*}

@subsubheading  Method Signatures::

@code{update-instance-for-redefined-class}  @i{@r{(}@i{instance} @b{standard-object}@r{)}
		added-slots discarded-slots
		property-list
		@r{&rest} initargs}

@subsubheading  Arguments and Values::

@i{instance}---an @i{object}.

@i{added-slots}---a @i{list}.

@i{discarded-slots}---a @i{list}.

@i{property-list}---a @i{list}.

@i{initargs}---an @i{initialization argument list}.

@i{result}---an @i{object}.

@subsubheading  Description::

The @i{generic function} @b{update-instance-for-redefined-class} 
is not intended to be called by programmers. Programmers may write
@i{methods} for it.  The @i{generic function} 
@b{update-instance-for-redefined-class} is called by the mechanism
activated by @b{make-instances-obsolete}.

The system-supplied primary @i{method} on 
@b{update-instance-for-redefined-class} checks the validity of
@i{initargs} and signals an error if an @i{initarg}
is supplied that is not declared as valid.  This @i{method} then
initializes @i{slots} with values according to the @i{initargs},
and initializes the newly @i{added-slots} with values according
to their @t{:initform} forms.  It does this by calling the generic
function @b{shared-initialize} with the following arguments: 
the @i{instance},
a list of names of the newly @i{added-slots} to @i{instance},
and the @i{initargs}
it received.  Newly @i{added-slots} are those @i{local slots} for which
no @i{slot} of the same name exists in the old version of the @i{class}.

When @b{make-instances-obsolete} is invoked or when a @i{class} has been
redefined and an @i{instance} is being updated, a @i{property-list} is created
that captures the slot names and values of all the @i{discarded-slots} with
values in the original @i{instance}.  The structure of the 
@i{instance} is
transformed so that it conforms to the current class definition.  The
arguments to @b{update-instance-for-redefined-class} are this
transformed @i{instance}, a list of @i{added-slots} to the
@i{instance}, a list @i{discarded-slots} from the
@i{instance}, and the @i{property-list} 
containing the slot names and values for
@i{slots} that were discarded and had values.  Included in this list of
discarded @i{slots} are @i{slots} that were local in the old @i{class} and are
shared in the new @i{class}.

The value returned by @b{update-instance-for-redefined-class} is ignored.

@subsubheading  Examples::

@example

 (defclass position () ())

 (defclass x-y-position (position)
     ((x :initform 0 :accessor position-x)
      (y :initform 0 :accessor position-y)))

;;; It turns out polar coordinates are used more than Cartesian 
;;; coordinates, so the representation is altered and some new
;;; accessor methods are added.

 (defmethod update-instance-for-redefined-class :before
    ((pos x-y-position) added deleted plist &key)
   ;; Transform the x-y coordinates to polar coordinates
   ;; and store into the new slots.
   (let ((x (getf plist 'x))
         (y (getf plist 'y)))
     (setf (position-rho pos) (sqrt (+ (* x x) (* y y)))
           (position-theta pos) (atan y x))))

 (defclass x-y-position (position)
     ((rho :initform 0 :accessor position-rho)
      (theta :initform 0 :accessor position-theta)))

;;; All instances of the old x-y-position class will be updated
;;; automatically.

;;; The new representation is given the look and feel of the old one.

 (defmethod position-x ((pos x-y-position))  
    (with-slots (rho theta) pos (* rho (cos theta))))

 (defmethod (setf position-x) (new-x (pos x-y-position))
    (with-slots (rho theta) pos
      (let ((y (position-y pos)))
        (setq rho (sqrt (+ (* new-x new-x) (* y y)))
              theta (atan y new-x))
        new-x)))

 (defmethod position-y ((pos x-y-position))
    (with-slots (rho theta) pos (* rho (sin theta))))

 (defmethod (setf position-y) (new-y (pos x-y-position))
    (with-slots (rho theta) pos
      (let ((x (position-x pos)))
        (setq rho (sqrt (+ (* x x) (* new-y new-y)))
              theta (atan new-y x))
        new-y)))

@end example

@subsubheading  Exceptional Situations::
The system-supplied primary @i{method} on 
@b{update-instance-for-redefined-class} signals an error if an
@i{initarg} is supplied that is not declared as valid.

@subsubheading  See Also::

@ref{make-instances-obsolete}
,
@ref{Shared-Initialize}
,
@ref{Redefining Classes},
@ref{Rules for Initialization Arguments},
@ref{Declaring the Validity of Initialization Arguments}

@subsubheading  Notes::

@i{Initargs} are declared as valid by using the @t{:initarg}
option to @b{defclass}, or by defining @i{methods} for
@b{update-instance-for-redefined-class} or @b{shared-initialize}.
The keyword name of each keyword parameter specifier in the @i{lambda list} of
any @i{method} defined on 
@b{update-instance-for-redefined-class} or 
@b{shared-initialize} is declared as a valid @i{initarg} name
for all @i{classes} for which that @i{method} is applicable.

@node change-class, slot-boundp, update-instance-for-redefined-class, Objects Dictionary
@subsection change-class                                    [Standard Generic Function]

@subsubheading  Syntax::

@code{change-class}  @i{instance new-class @r{&key} @r{&allow-other-keys}} @result{}  @i{instance}

@subsubheading  Method Signatures::

@code{change-class}  @i{@r{(}@i{instance} @b{standard-object}@r{)}
			 @r{(}@i{new-class} @b{standard-class}@r{)}
			 @r{&rest} initargs}

@code{change-class}  @i{@r{(}@i{instance} @b{t}@r{)}
		         @r{(}@i{new-class} @b{symbol}@r{)}
			 @r{&rest} initargs}

@subsubheading  Arguments and Values::

@i{instance}---an @i{object}.

@i{new-class}---a @i{class designator}.

@i{initargs}---an @i{initialization argument list}.

@subsubheading  Description::

The @i{generic function} @b{change-class} changes the 
@i{class} of an @i{instance} to @i{new-class}.  
It destructively modifies and returns the @i{instance}.

If in the old @i{class} there is any @i{slot} of the 
same name as a local @i{slot} in the @i{new-class}, 
the value of that @i{slot} is retained.  This means that if 
the @i{slot} has a value, the value returned by @b{slot-value}
after @b{change-class} is invoked is @b{eql} to the
value returned by @b{slot-value} before @b{change-class} is
invoked.  Similarly, if the @i{slot} was unbound, it remains
unbound.  The other @i{slots} are initialized as described in 
@ref{Changing the Class of an Instance}.

After completing all other actions, @b{change-class} invokes
@b{update-instance-for-different-class}.  The
generic function @b{update-instance-for-different-class} can be used
to assign values to slots in the transformed instance.

See @ref{Initializing Newly Added Local Slots (Changing the Class of an Instance)}.

If the second of the above @i{methods} is selected, 
that @i{method} invokes @b{change-class} 
on @i{instance}, @t{(find-class @i{new-class})},
and the @i{initargs}.

@subsubheading  Examples::

@example

 (defclass position () ())

 (defclass x-y-position (position)
     ((x :initform 0 :initarg :x)
      (y :initform 0 :initarg :y)))

 (defclass rho-theta-position (position)
     ((rho :initform 0)
      (theta :initform 0)))

 (defmethod update-instance-for-different-class :before ((old x-y-position) 
                                                         (new rho-theta-position)
                                                         &key)
   ;; Copy the position information from old to new to make new
   ;; be a rho-theta-position at the same position as old.
   (let ((x (slot-value old 'x))
         (y (slot-value old 'y)))
     (setf (slot-value new 'rho) (sqrt (+ (* x x) (* y y)))
           (slot-value new 'theta) (atan y x))))

;;; At this point an instance of the class x-y-position can be
;;; changed to be an instance of the class rho-theta-position using
;;; change-class:

 (setq p1 (make-instance 'x-y-position :x 2 :y 0))

 (change-class p1 'rho-theta-position)

;;; The result is that the instance bound to p1 is now an instance of
;;; the class rho-theta-position.   The update-instance-for-different-class
;;; method performed the initialization of the rho and theta slots based
;;; on the value of the x and y slots, which were maintained by
;;; the old instance.

@end example

@subsubheading  See Also::

@ref{update-instance-for-different-class}
,
@ref{Changing the Class of an Instance}

@subsubheading  Notes::

The generic function @b{change-class} has several semantic
difficulties.  First, it performs a destructive operation that can be
invoked within a @i{method} on an @i{instance} that was used to select that
@i{method}. 
When multiple @i{methods} are involved because @i{methods} are being
combined, the @i{methods} currently executing or about to be executed may
no longer be applicable.  Second, some implementations might use
compiler optimizations of slot @i{access}, and when the @i{class} of an
@i{instance} is changed the assumptions the compiler made might be
violated.  This implies that a programmer must not use
@b{change-class} inside a @i{method} if any 
@i{methods} for that @i{generic function}
@i{access} any @i{slots}, or the results are undefined.

@node slot-boundp, slot-exists-p, change-class, Objects Dictionary
@subsection slot-boundp                                                      [Function]

@code{slot-boundp}  @i{instance slot-name} @result{}  @i{generalized-boolean}

@subsubheading  Arguments and Values::

@i{instance}---an @i{object}.

@i{slot-name}---a @i{symbol} naming a @i{slot} of @i{instance}.

@i{generalized-boolean}---a @i{generalized boolean}.

@subsubheading  Description::

Returns @i{true} if the @i{slot} named @i{slot-name} in @i{instance} is bound;
otherwise, returns @i{false}.

@subsubheading  Exceptional Situations::

If no @i{slot} of the @i{name} @i{slot-name} exists in the 
@i{instance}, @b{slot-missing} is called as follows:

@example
 (slot-missing (class-of @i{instance})
               @i{instance}
               @i{slot-name}
               'slot-boundp)
@end example

(If @b{slot-missing} is invoked and returns a value,
a @i{boolean equivalent} to its @i{primary value} 
is returned by @b{slot-boundp}.)

The specific behavior depends on @i{instance}'s @i{metaclass}.
An error is never signaled if @i{instance} has @i{metaclass} @b{standard-class}.
An error is always signaled if @i{instance} has @i{metaclass} @b{built-in-class}.
The consequences are undefined if @i{instance} has any other @i{metaclass}--an error
might or might not be signaled in this situation.  Note in particular that the behavior
for @i{conditions} and @i{structures} is not specified.

@subsubheading  See Also::

@ref{slot-makunbound}
,
@ref{slot-missing}

@subsubheading  Notes::

The @i{function} @b{slot-boundp} allows for writing 
@i{after methods} on @b{initialize-instance} in order to initialize only
those @i{slots} that have not already been bound.

  Although no @i{implementation} is required to do so,
  implementors are strongly encouraged to implement the @i{function} @b{slot-boundp} using 
  the @i{function} @t{slot-boundp-using-class} described in the @i{Metaobject Protocol}.

@node slot-exists-p, slot-makunbound, slot-boundp, Objects Dictionary
@subsection slot-exists-p                                                    [Function]

@code{slot-exists-p}  @i{object slot-name} @result{}  @i{generalized-boolean}

@subsubheading  Arguments and Values::

@i{object}---an @i{object}.

@i{slot-name}---a @i{symbol}.

@i{generalized-boolean}---a @i{generalized boolean}.

@subsubheading  Description::

Returns @i{true} if the @i{object} has
a @i{slot} named @i{slot-name}.

@subsubheading  Affected By::

@b{defclass},
@b{defstruct}

@subsubheading  See Also::

@ref{defclass}
,
@ref{slot-missing}

@subsubheading  Notes::

  Although no @i{implementation} is required to do so,
  implementors are strongly encouraged to implement the @i{function} @b{slot-exists-p} using 
  the @i{function} @t{slot-exists-p-using-class} described in the @i{Metaobject Protocol}.

@node slot-makunbound, slot-missing, slot-exists-p, Objects Dictionary
@subsection slot-makunbound                                                  [Function]

@code{slot-makunbound}  @i{instance slot-name} @result{}  @i{instance}

@subsubheading  Arguments and Values::

@i{instance} -- instance.

@i{Slot-name}---a @i{symbol}.

@subsubheading  Description::

The @i{function} @b{slot-makunbound} restores a @i{slot} 
of the name @i{slot-name} in an @i{instance} to
the unbound state.

@subsubheading  Exceptional Situations::

If no @i{slot} of the name @i{slot-name} exists in the 
@i{instance}, @b{slot-missing} is called as follows:

@example
(slot-missing (class-of @i{instance})
              @i{instance}
              @i{slot-name}
              'slot-makunbound)
@end example

(Any values returned by @b{slot-missing} in this case are
ignored by @b{slot-makunbound}.)

The specific behavior depends on @i{instance}'s @i{metaclass}.
An error is never signaled if @i{instance} has @i{metaclass} @b{standard-class}.
An error is always signaled if @i{instance} has @i{metaclass} @b{built-in-class}.
The consequences are undefined if @i{instance} has any other @i{metaclass}--an error
might or might not be signaled in this situation.  Note in particular that the behavior
for @i{conditions} and @i{structures} is not specified.

@subsubheading  See Also::

@ref{slot-boundp}
,
@ref{slot-missing}

@subsubheading  Notes::

  Although no @i{implementation} is required to do so,
  implementors are strongly encouraged to implement the @i{function} @b{slot-makunbound} using 
  the @i{function} @t{slot-makunbound-using-class} described in the @i{Metaobject Protocol}.

@node slot-missing, slot-unbound, slot-makunbound, Objects Dictionary
@subsection slot-missing                                    [Standard Generic Function]

@subsubheading  Syntax::

@code{slot-missing}  @i{class object slot-name operation @r{&optional} new-value} @result{}  @i{@{@i{result}@}*}

@subsubheading  Method Signatures::

@code{slot-missing}  @i{@r{(}@i{class} @b{t}@r{)}
				   object slot-name
			     operation @r{&optional} new-value}

@subsubheading  Arguments and Values::

@i{class}---the @i{class} of @i{object}.

@i{object}---an @i{object}.

@i{slot-name}---a @i{symbol} (the @i{name} of a would-be @i{slot}).

@i{operation}---one of the @i{symbols}
		    @b{setf},
		    @b{slot-boundp},
		    @b{slot-makunbound},
		 or @b{slot-value}.

@i{new-value}---an @i{object}.

@i{result}---an @i{object}.

@subsubheading  Description::

The generic function @b{slot-missing} is invoked when an attempt is
made to @i{access} a @i{slot} in an @i{object} whose 
@i{metaclass} is @b{standard-class}
and the @i{slot} of the name @i{slot-name}
is not a @i{name} of a
@i{slot} in that @i{class}. 
The default @i{method} signals an error.

The generic function @b{slot-missing} is not intended to be called by
programmers.  Programmers may write @i{methods} for it.

The generic function @b{slot-missing} may be called during
evaluation of @b{slot-value}, @t{(setf slot-value)}, 
@b{slot-boundp}, and @b{slot-makunbound}.  For each
of these operations the corresponding @i{symbol} 
for the @i{operation}
argument is @b{slot-value}, @b{setf}, @b{slot-boundp},
and @b{slot-makunbound} respectively.

The optional @i{new-value} argument to @b{slot-missing} is used
when the operation is attempting to set the value of the @i{slot}.

If @b{slot-missing} returns, its values will be treated as follows:

@table @asis

@item @t{*}  
If the @i{operation} is @b{setf} or @b{slot-makunbound},
any @i{values} will be ignored by the caller.

@item @t{*}  
If the @i{operation} is @b{slot-value},
only the @i{primary value} will be used by the caller,
and all other values will be ignored.

@item @t{*}  
If the @i{operation} is @b{slot-boundp},
any @i{boolean equivalent} of the @i{primary value}
of the @i{method} might be is used,
and all other values will be ignored.
@end table

@subsubheading  Exceptional Situations::

The default @i{method} on @b{slot-missing} 
signals an error of @i{type} @b{error}.

@subsubheading  See Also::

@ref{defclass}
,
@ref{slot-exists-p}
,
@ref{slot-value}

@subsubheading  Notes::

The set of arguments (including the @i{class} of the instance) facilitates
defining methods on the metaclass for @b{slot-missing}.

@node slot-unbound, slot-value, slot-missing, Objects Dictionary
@subsection slot-unbound                                    [Standard Generic Function]

@subsubheading  Syntax::

@code{slot-unbound}  @i{class instance slot-name} @result{}  @i{@{@i{result}@}*}

@subsubheading  Method Signatures::

@code{slot-unbound}  @i{@r{(}@i{class} @b{t}@r{)}
		       instance slot-name}

@subsubheading  Arguments and Values::

@i{class}---the @i{class} of the @i{instance}.

@i{instance}---the @i{instance} in which an attempt
		   was made to @i{read} the @i{unbound} @i{slot}.

@i{slot-name}---the @i{name} of the @i{unbound} @i{slot}.

@i{result}---an @i{object}.

@subsubheading  Description::

The generic function @b{slot-unbound} is called when an
unbound @i{slot} is read in
an @i{instance} whose metaclass is @b{standard-class}.
The default @i{method} signals an error 

of @i{type} @b{unbound-slot}.
The name slot of the 
@b{unbound-slot} @i{condition} is initialized
  to the name of the offending variable, and the instance slot
  of the @b{unbound-slot} @i{condition} is initialized to the offending instance.

The generic function @b{slot-unbound} is not intended to be called
by programmers.  Programmers may write @i{methods} for it.
The @i{function} @b{slot-unbound} is called only 
indirectly by @b{slot-value}.

If @b{slot-unbound} returns, 
only the @i{primary value} will be used by the caller,
and all other values will be ignored.

@subsubheading  Exceptional Situations::

The default @i{method} on @b{slot-unbound}
signals an error of @i{type} @b{unbound-slot}.

@subsubheading  See Also::

@ref{slot-makunbound}

@subsubheading  Notes::

An unbound @i{slot} may occur if no @t{:initform} form was
specified for the @i{slot} and the @i{slot} value has not been set,
or if @b{slot-makunbound} has been called on the @i{slot}.

@node slot-value, method-qualifiers, slot-unbound, Objects Dictionary
@subsection slot-value                                                       [Function]

@code{slot-value}  @i{object slot-name} @result{}  @i{value}

@subsubheading  Arguments and Values::

@i{object}---an @i{object}.

@i{name}---a @i{symbol}.

@i{value}---an @i{object}.

@subsubheading  Description::

The @i{function} @b{slot-value} returns the @i{value} of the @i{slot}
named @i{slot-name} in the @i{object}.
If there is no @i{slot} named @i{slot-name}, @b{slot-missing} is called.
If the @i{slot} is unbound, @b{slot-unbound} is called.

The macro @b{setf} can be used with @b{slot-value} 
to change the value of a @i{slot}. 

@subsubheading  Examples::

@example
 (defclass foo () 
   ((a :accessor foo-a :initarg :a :initform 1)
    (b :accessor foo-b :initarg :b)
    (c :accessor foo-c :initform 3)))
@result{}  #<STANDARD-CLASS FOO 244020371>
 (setq foo1 (make-instance 'foo :a 'one :b 'two))
@result{}  #<FOO 36325624>
 (slot-value foo1 'a) @result{}  ONE
 (slot-value foo1 'b) @result{}  TWO
 (slot-value foo1 'c) @result{}  3
 (setf (slot-value foo1 'a) 'uno) @result{}  UNO
 (slot-value foo1 'a) @result{}  UNO
 (defmethod foo-method ((x foo))
   (slot-value x 'a))
@result{}  #<STANDARD-METHOD FOO-METHOD (FOO) 42720573>
 (foo-method foo1) @result{}  UNO
@end example

@subsubheading  Exceptional Situations::

If an attempt is made to read a @i{slot} and no @i{slot} of
the name @i{slot-name} exists in the @i{object}, 
@b{slot-missing} is called as follows:

@example
 (slot-missing (class-of @i{instance})
               @i{instance}
               @i{slot-name}
               'slot-value)
@end example

(If @b{slot-missing} is invoked, its @i{primary value} 
 is returned by @b{slot-value}.)

If an attempt is made to write a @i{slot} and no @i{slot} of
the name @i{slot-name} exists in the @i{object},
@b{slot-missing} is called as follows:

@example
 (slot-missing (class-of @i{instance})
               @i{instance}
               @i{slot-name}
               'setf
               @i{new-value})
@end example

(If @b{slot-missing} returns in this case, any @i{values} are ignored.)

The specific behavior depends on @i{object}'s @i{metaclass}.
An error is never signaled if @i{object} has @i{metaclass} @b{standard-class}.
An error is always signaled if @i{object} has @i{metaclass} @b{built-in-class}.
The consequences are 
unspecified
if @i{object} has any other @i{metaclass}--an error
might or might not be signaled in this situation.  Note in particular that the behavior
for @i{conditions} and @i{structures} is not specified.

@subsubheading  See Also::

@ref{slot-missing}
,
@ref{slot-unbound}
,
@ref{with-slots}

@subsubheading  Notes::

  Although no @i{implementation} is required to do so,
  implementors are strongly encouraged to implement the @i{function} @b{slot-value} using 
  the @i{function} @t{slot-value-using-class} described in the @i{Metaobject Protocol}.

Implementations may optimize @b{slot-value} by compiling it inline.

@node method-qualifiers, no-applicable-method, slot-value, Objects Dictionary
@subsection method-qualifiers                               [Standard Generic Function]

@subsubheading  Syntax::

@code{method-qualifiers}  @i{method} @result{}  @i{qualifiers}

@subsubheading  Method Signatures::

@code{method-qualifiers}  @i{@r{(}@i{method} @b{standard-method}@r{)}}

@subsubheading  Arguments and Values::

@i{method}---a @i{method}.

@i{qualifiers}---a @i{proper list}.

@subsubheading  Description::

Returns a @i{list} of the @i{qualifiers} of the @i{method}.

@subsubheading  Examples::

@example
 (defmethod some-gf :before ((a integer)) a)
@result{}  #<STANDARD-METHOD SOME-GF (:BEFORE) (INTEGER) 42736540>
 (method-qualifiers *) @result{}  (:BEFORE)
@end example

@subsubheading  See Also:: 

@ref{define-method-combination}

@node no-applicable-method, no-next-method, method-qualifiers, Objects Dictionary
@subsection no-applicable-method                            [Standard Generic Function]

@subsubheading  Syntax::

@code{no-applicable-method}  @i{generic-function @r{&rest} function-arguments} @result{}  @i{@{@i{result}@}*}

@subsubheading  Method Signatures::

@code{no-applicable-method}  @i{@r{(}@i{generic-function} @b{t}@r{)}
				     @r{&rest} function-arguments}

@subsubheading  Arguments and Values::

@i{generic-function}---a @i{generic function} 
			   on which no @i{applicable method} was found.  

@i{function-arguments}---@i{arguments} to the @i{generic-function}.

@i{result}---an @i{object}.

@subsubheading  Description::

The generic function @b{no-applicable-method} is called when a
@i{generic function} 
is invoked
and no @i{method} on that @i{generic function} is applicable.
The @i{default method} signals an error.

The generic function @b{no-applicable-method} is not intended
to be called by programmers.  Programmers may write @i{methods} for it.

@subsubheading  Exceptional Situations::

The default @i{method} signals an error of @i{type} @b{error}.

@subsubheading  See Also::

@node no-next-method, remove-method, no-applicable-method, Objects Dictionary
@subsection no-next-method                                  [Standard Generic Function]

@subsubheading  Syntax::

@code{no-next-method}  @i{generic-function method @r{&rest} args} @result{}  @i{@{@i{result}@}*}

@subsubheading  Method Signatures::

@code{no-next-method}  @i{@r{(}@i{generic-function} @b{standard-generic-function}@r{)}
			       @r{(}@i{method} @b{standard-method}@r{)}
			       @r{&rest} args}

@subsubheading  Arguments and Values::

@i{generic-function} -- @i{generic function} to which @i{method} belongs.

@i{method} -- @i{method} that contained the call to
		  @b{call-next-method} for which there is no next @i{method}.

@i{args} -- arguments to @b{call-next-method}.

@i{result}---an @i{object}.

@subsubheading  Description::

The @i{generic function} @b{no-next-method} is called by @b{call-next-method} 
when there is no @i{next method}.

The @i{generic function} @b{no-next-method} is not intended to be called by programmers.
Programmers may write @i{methods} for it.

@subsubheading  Exceptional Situations::

The system-supplied @i{method} on @b{no-next-method} 
signals an error of @i{type} @b{error}. 
[Editorial Note by KMP: perhaps control-error??]

@subsubheading  See Also::

@ref{call-next-method}

@node remove-method, make-instance, no-next-method, Objects Dictionary
@subsection remove-method                                   [Standard Generic Function]

@subsubheading  Syntax::

@code{remove-method}  @i{generic-function method} @result{}  @i{generic-function}

@subsubheading  Method Signatures::

@code{remove-method}  @i{@r{(}@i{generic-function} @b{standard-generic-function}@r{)}
			      method}

@subsubheading  Arguments and Values::

@i{generic-function}---a @i{generic function}.

@i{method}---a @i{method}.

@subsubheading  Description::

The @i{generic function} @b{remove-method} removes a @i{method} from @i{generic-function}
by modifying the @i{generic-function} (if necessary).

@b{remove-method} must not signal an error if the @i{method} 
is not one of the @i{methods} on the @i{generic-function}.

@subsubheading  See Also::

@ref{find-method}

@node make-instance, make-instances-obsolete, remove-method, Objects Dictionary
@subsection make-instance                                   [Standard Generic Function]

@subsubheading  Syntax::

@code{make-instance}  @i{class @r{&rest} initargs @r{&key} @r{&allow-other-keys}} @result{}  @i{instance}

@subsubheading  Method Signatures::

@code{make-instance}  @i{@r{(}@i{class} @b{standard-class}@r{)} @r{&rest} initargs}

@code{make-instance}  @i{@r{(}@i{class} @b{symbol}@r{)} @r{&rest} initargs}

@subsubheading  Arguments and Values::

@i{class}---a @i{class},
	     or a @i{symbol} that names a @i{class}.

@i{initargs}---an @i{initialization argument list}.

@i{instance}---a @i{fresh} @i{instance} of @i{class} @i{class}.

@subsubheading  Description::

The @i{generic function} @b{make-instance} 
creates and returns a new @i{instance} of the given @i{class}.

If the second of the above @i{methods} is selected, 
that @i{method} invokes @b{make-instance} on the arguments
@t{(find-class @i{class})} and @i{initargs}.

The initialization arguments are checked within @b{make-instance}.

The @i{generic function} @b{make-instance} 
may be used as described in @ref{Object Creation and Initialization}.

@subsubheading  Exceptional Situations::

If any of the initialization arguments has not
been declared as valid, an error of @i{type} @b{error} is signaled.

@subsubheading  See Also::

@ref{defclass}
,
@ref{class-of}
,
@ref{allocate-instance}
,
@ref{Initialize-Instance}
,
@ref{Object Creation and Initialization}

@node make-instances-obsolete, make-load-form, make-instance, Objects Dictionary
@subsection make-instances-obsolete                         [Standard Generic Function]

@subsubheading  Syntax::

@code{make-instances-obsolete}  @i{class} @result{}  @i{class}

@subsubheading  Method Signatures::

@code{make-instances-obsolete}  @i{@r{(}@i{class} @b{standard-class}@r{)}}

@code{make-instances-obsolete}  @i{@r{(}@i{class} @b{symbol}@r{)}}

@subsubheading  Arguments and Values::

@i{class}---a @i{class designator}.

@subsubheading  Description::

The @i{function} @b{make-instances-obsolete} has the effect of
initiating the process of updating the instances of the
@i{class}. During updating, the generic function
@b{update-instance-for-redefined-class} will be invoked.

The generic function @b{make-instances-obsolete} is invoked
automatically by the system when @b{defclass} has been used to
redefine an existing standard class and the set of local 
@i{slots} @i{accessible} in an
instance is changed or the order of @i{slots} in storage is changed.  It
can also be explicitly invoked by the user.

If the second of the above @i{methods} is selected, that 
@i{method} invokes
@b{make-instances-obsolete} on @t{(find-class @i{class})}.

@subsubheading  Examples::

@subsubheading  See Also::

@ref{update-instance-for-redefined-class}
,
@ref{Redefining Classes}

@node make-load-form, make-load-form-saving-slots, make-instances-obsolete, Objects Dictionary
@subsection make-load-form                                  [Standard Generic Function]

@subsubheading  Syntax::

@code{make-load-form}  @i{object @r{&optional} environment} @result{}  @i{creation-form @r{[}, initialization-form @r{]}}

@subsubheading  Method Signatures::

@code{make-load-form}  @i{@r{(}@i{object} @b{standard-object}@r{)}  @r{&optional} environment}

@code{make-load-form}  @i{@r{(}@i{object} @b{structure-object}@r{)} @r{&optional} environment}

@code{make-load-form}  @i{@r{(}@i{object} @b{condition}@r{)}        @r{&optional} environment}

@code{make-load-form}  @i{@r{(}@i{object} @b{class}@r{)}            @r{&optional} environment}

@subsubheading  Arguments and Values::

@i{object}---an @i{object}.

@i{environment}---an @i{environment object}.

@i{creation-form}---a @i{form}.

@i{initialization-form}---a @i{form}.

@subsubheading  Description::

The @i{generic function} @b{make-load-form} creates and returns 
one or two @i{forms},
     a @i{creation-form}
 and an @i{initialization-form},
that enable @b{load} to construct an @i{object}
equivalent to @i{object}.
@i{Environment} is an @i{environment object} 
corresponding to the @i{lexical environment} 
in which the @i{forms} will be processed.

The @i{file compiler} calls @b{make-load-form} to process certain
@i{classes} of @i{literal objects}; see @ref{Additional Constraints on Externalizable Objects}.

@i{Conforming programs} may call @b{make-load-form} directly,
providing @i{object} is a @i{generalized instance} of
@b{standard-object}, @b{structure-object}, 
or @b{condition}.

The creation form is a @i{form} that, when evaluated at
@b{load} time, should return an @i{object} that 
is equivalent to @i{object}.  The exact meaning of
equivalent depends on the @i{type} of @i{object} 
and is up to the programmer who defines a @i{method} for
@b{make-load-form};
see @ref{Literal Objects in Compiled Files}.

The initialization form is a @i{form} that, when evaluated at @b{load} time, 
should perform further initialization of the @i{object}.  
The value returned by the initialization form is ignored.
If @b{make-load-form}
returns only one value, 
the initialization form is @b{nil}, which has no effect.
If @i{object} appears as a constant in the initialization form,
at @b{load} time it will be replaced by the equivalent @i{object} 
constructed by the creation form;
this is how the further initialization gains access to the @i{object}.

Both the @i{creation-form} and the @i{initialization-form} may contain references
to any @i{externalizable object}.
However, there must not be any circular dependencies in creation forms.
An example of a circular dependency is when the creation form for the
object @t{X} contains a reference to the object @t{Y},
and the creation form for the object @t{Y} contains a reference to the object @t{X}.  
Initialization forms are not subject to any restriction against circular dependencies, 
which is the reason that initialization forms exist; 
see the example of circular data structures below.

The creation form for an @i{object} is always @i{evaluated} before the
initialization form for that @i{object}.  When either the creation form or
the initialization form references other @i{objects} that have not been
referenced earlier in the @i{file} being @i{compiled}, the @i{compiler} ensures
that all of the referenced @i{objects} have been created before @i{evaluating}
the referencing @i{form}.  When the referenced @i{object} is of a @i{type} which
the @i{file compiler} processes using @b{make-load-form},
this involves @i{evaluating}
the creation form returned for it.  (This is the reason for the
prohibition against circular references among creation forms).

Each initialization form is @i{evaluated} as soon as possible after its
associated creation form, as determined by data flow.  If the
initialization form for an @i{object} does not reference any other @i{objects}
not referenced earlier in the @i{file} and processed by 
the @i{file compiler}
using
@b{make-load-form}, the initialization form is evaluated immediately after
the creation form.  If a creation or initialization form F does contain
references to such @i{objects}, the creation forms for those other objects
are evaluated before F, and the initialization forms for those other
@i{objects} are also evaluated before F whenever they do not depend on the
@i{object} created or initialized by F.  Where these rules do not uniquely
determine an order of @i{evaluation} between two creation/initialization
forms, the order of @i{evaluation} is unspecified.

  While these creation and initialization forms are being evaluated, the
  @i{objects} are possibly in an uninitialized state, 
analogous to the state
  of an @i{object} 
between the time it has been created by @b{allocate-instance}
  and it has been processed fully by 
@b{initialize-instance}.  Programmers
  writing @i{methods} for 
@b{make-load-form} must take care in manipulating
  @i{objects} not to depend on 
@i{slots} that have not yet been initialized.

  It is @i{implementation-dependent}
whether @b{load} calls @b{eval} on the 
@i{forms} or does some
  other operation that has an equivalent effect.  For example, the
  @i{forms} might be translated into different but equivalent 
@i{forms} and
  then evaluated, they might be compiled and the resulting functions
  called by @b{load}, 
or they might be interpreted by a special-purpose
function different from @b{eval}.  
All that is required is that the
  effect be equivalent to evaluating the @i{forms}.

The @i{method} @i{specialized} on @b{class} returns a creation
@i{form} using the @i{name} of the @i{class} if the @i{class} has
a @i{proper name} in @i{environment}, signaling an error of @i{type} @b{error}
if it does not have a @i{proper name}.  @i{Evaluation} of the creation
@i{form} uses the @i{name} to find the @i{class} with that
@i{name}, as if by @i{calling} @b{find-class}.  If a @i{class}
with that @i{name} has not been defined, then a @i{class} may be
computed in an @i{implementation-defined} manner.  If a @i{class}
cannot be returned as the result of @i{evaluating} the creation
@i{form}, then an error of @i{type} @b{error} is signaled.

Both @i{conforming implementations} and @i{conforming programs} may
further @i{specialize} @b{make-load-form}.

@subsubheading  Examples::

@example
 (defclass obj ()
    ((x :initarg :x :reader obj-x)
     (y :initarg :y :reader obj-y)
     (dist :accessor obj-dist)))
@result{}  #<STANDARD-CLASS OBJ 250020030>
 (defmethod shared-initialize :after ((self obj) slot-names &rest keys)
   (declare (ignore slot-names keys))
   (unless (slot-boundp self 'dist)
     (setf (obj-dist self)
           (sqrt (+ (expt (obj-x self) 2) (expt (obj-y self) 2))))))
@result{}  #<STANDARD-METHOD SHARED-INITIALIZE (:AFTER) (OBJ T) 26266714>
 (defmethod make-load-form ((self obj) &optional environment)
   (declare (ignore environment))
   ;; Note that this definition only works because X and Y do not
   ;; contain information which refers back to the object itself.
   ;; For a more general solution to this problem, see revised example below.
   `(make-instance ',(class-of self)
                   :x ',(obj-x self) :y ',(obj-y self)))
@result{}  #<STANDARD-METHOD MAKE-LOAD-FORM (OBJ) 26267532>
 (setq obj1 (make-instance 'obj :x 3.0 :y 4.0)) @result{}  #<OBJ 26274136>
 (obj-dist obj1) @result{}  5.0
 (make-load-form obj1) @result{}  (MAKE-INSTANCE 'OBJ :X '3.0 :Y '4.0)
@end example

In the above example, an equivalent @i{instance} of @t{obj} is
reconstructed by using the values of two of its @i{slots}.  
The value of the third @i{slot} is derived from those two values.

Another way to write the @b{make-load-form} @i{method}
in that example is to use @b{make-load-form-saving-slots}.
The code it generates might yield a slightly different result 
from the @b{make-load-form} @i{method} shown above,
but the operational effect will be the same.  For example:

@example
 ;; Redefine method defined above.
 (defmethod make-load-form ((self obj) &optional environment)
    (make-load-form-saving-slots self
                                 :slot-names '(x y)
                                 :environment environment))
@result{}  #<STANDARD-METHOD MAKE-LOAD-FORM (OBJ) 42755655>
 ;; Try MAKE-LOAD-FORM on object created above.
 (make-load-form obj1)
@result{}  (ALLOCATE-INSTANCE '#<STANDARD-CLASS OBJ 250020030>),
    (PROGN
      (SETF (SLOT-VALUE '#<OBJ 26274136> 'X) '3.0)
      (SETF (SLOT-VALUE '#<OBJ 26274136> 'Y) '4.0)
      (INITIALIZE-INSTANCE '#<OBJ 26274136>))
@end example

In the following example, @i{instances} of @t{my-frob} are ``interned'' 
in some way.  An equivalent @i{instance} is reconstructed by using the 
value of the name slot as a key for searching existing @i{objects}.
In this case the programmer has chosen to create a new @i{object} 
if no existing @i{object} is found; alternatively an error could 
have been signaled in that case.

@example
 (defclass my-frob ()
    ((name :initarg :name :reader my-name)))
 (defmethod make-load-form ((self my-frob) &optional environment)
   (declare (ignore environment))
   `(find-my-frob ',(my-name self) :if-does-not-exist :create))
@end example

In the following example, the data structure to be dumped is circular, 
because each parent has a list of its children and each child has a reference
back to its parent.  If @b{make-load-form} is called on one   
@i{object} in such a structure,  the creation form creates an equivalent 
@i{object} and fills in the children slot, which forces creation of equivalent
@i{objects} for all of its children, grandchildren, etc.  At this point
none of the parent @i{slots} have been filled in.  
The initialization form fills in the parent @i{slot}, which forces creation 
of an equivalent @i{object} for the parent if it was not already created.
Thus the entire tree is recreated at @b{load} time.  
At compile time, @b{make-load-form} is called once for each @i{object} 
in the tree.  
All of the creation forms are evaluated,
in @i{implementation-dependent} order,
and then all of the initialization forms are evaluated, 
also in @i{implementation-dependent} order.

@example
 (defclass tree-with-parent () ((parent :accessor tree-parent)
                                (children :initarg :children)))
 (defmethod make-load-form ((x tree-with-parent) &optional environment)
   (declare (ignore environment))
   (values
     ;; creation form
     `(make-instance ',(class-of x) :children ',(slot-value x 'children))
     ;; initialization form
     `(setf (tree-parent ',x) ',(slot-value x 'parent))))
@end example

In the following example, the data structure to be dumped has no special
properties and an equivalent structure can be reconstructed
simply by reconstructing the @i{slots}' contents.

@example
 (defstruct my-struct a b c)
 (defmethod make-load-form ((s my-struct) &optional environment)
    (make-load-form-saving-slots s :environment environment))
@end example

@subsubheading  Exceptional Situations::

The @i{methods} @i{specialized} on 
     @b{standard-object},
     @b{structure-object},
 and @b{condition}
all signal an error of @i{type} @b{error}.

It is @i{implementation-dependent} whether @i{calling}
@b{make-load-form} on a @i{generalized instance} of a
@i{system class} signals an error or returns creation and
initialization @i{forms}.

@subsubheading  See Also::

@ref{compile-file}
, 
@ref{make-load-form-saving-slots}
,
@ref{Additional Constraints on Externalizable Objects}
@ref{Evaluation},
@ref{Compilation}

@subsubheading  Notes::

The @i{file compiler}
calls @b{make-load-form} in specific circumstances
detailed in @ref{Additional Constraints on Externalizable Objects}.

Some @i{implementations} may provide facilities for defining new
@i{subclasses} of @i{classes} which are specified as
@i{system classes}.  (Some likely candidates include
@b{generic-function}, @b{method}, and @b{stream}).  Such
@i{implementations} should document how the @i{file compiler} processes
@i{instances} of such @i{classes} when encountered as
@i{literal objects}, and should document any relevant @i{methods}
for @b{make-load-form}.

@node make-load-form-saving-slots, with-accessors, make-load-form, Objects Dictionary
@subsection make-load-form-saving-slots                                      [Function]

@code{make-load-form-saving-slots}  @i{object @r{&key} slot-names environment}@*
   @result{}  @i{creation-form, initialization-form}

@subsubheading  Arguments and Values::

@i{object}---an @i{object}.

@i{slot-names}---a @i{list}.

@i{environment}---an @i{environment object}.

@i{creation-form}---a @i{form}.

@i{initialization-form}---a @i{form}.

@subsubheading  Description::

Returns @i{forms} that, when @i{evaluated}, will construct an
@i{object} equivalent to @i{object}, without @i{executing}
@i{initialization forms}.  The @i{slots} in the new @i{object}
that correspond to initialized @i{slots} in @i{object} are
initialized using the values from @i{object}.  Uninitialized @i{slots}
in @i{object} are not initialized in the new @i{object}.
@b{make-load-form-saving-slots} works for any @i{instance} of
@b{standard-object} or @b{structure-object}.

@i{Slot-names} is a @i{list} of the names of the 
@i{slots} to preserve. If @i{slot-names} is not
supplied, its value is all of the @i{local slots}.  

@b{make-load-form-saving-slots} returns two values,
thus it can deal with circular structures.
Whether the result is useful in an application depends on
whether the @i{object}'s @i{type} and slot contents
fully capture the application's idea of the @i{object}'s state.

@i{Environment} is the environment in which the forms will be processed.

@subsubheading  See Also::

@ref{make-load-form}
,
@ref{make-instance}
,
@ref{setf}
,
@ref{slot-value}
,
@ref{slot-makunbound}

@subsubheading  Notes::

@b{make-load-form-saving-slots} can be useful in user-written
@b{make-load-form} methods.

When the @i{object} is an @i{instance} of @b{standard-object},
@b{make-load-form-saving-slots} could return a creation form that
@i{calls} @b{allocate-instance} and an initialization form that
contains @i{calls} to @b{setf} of @b{slot-value} and
@b{slot-makunbound}, though other @i{functions} of similar effect
might actually be used.

@node with-accessors, with-slots, make-load-form-saving-slots, Objects Dictionary
@subsection with-accessors                                                      [Macro]

@code{with-accessors}  @i{@r{@r{(}@{@i{slot-entry}@}*@r{)}} 
		   instance-form
 		   @{@i{declaration}@}* @{@i{form}@}*}@*
   @result{}  @i{@{@i{result}@}*}

@w{@i{slot-entry} ::=@r{(}variable-name accessor-name @r{)}}

@subsubheading  Arguments and Values::

@i{variable-name}---a @i{variable name}; not evaluated.

@i{accessor-name}---a @i{function name}; not evaluated.

@i{instance-form}---a @i{form}; evaluated.

@i{declaration}---a @b{declare} @i{expression}; not evaluated.

@i{forms}---an @i{implicit progn}.

@i{results}---the @i{values} returned by the @i{forms}.

@subsubheading  Description::

Creates a lexical environment in which
the slots specified by
@i{slot-entry} are lexically available through their accessors as if
they were variables.  The macro @b{with-accessors} invokes the
appropriate accessors to @i{access} the @i{slots} specified
by @i{slot-entry}.  Both @b{setf}
and @b{setq} can be used to set the value of the @i{slot}.

@subsubheading  Examples::

@example
 (defclass thing ()
           ((x :initarg :x :accessor thing-x)
            (y :initarg :y :accessor thing-y)))
@result{}  #<STANDARD-CLASS THING 250020173>
 (defmethod (setf thing-x) :before (new-x (thing thing))
   (format t "~&Changing X from ~D to ~D in ~S.~
           (thing-x thing) new-x thing))
 (setq thing1 (make-instance 'thing :x 1 :y 2)) @result{}  #<THING 43135676>
 (setq thing2 (make-instance 'thing :x 7 :y 8)) @result{}  #<THING 43147374>
 (with-accessors ((x1 thing-x) (y1 thing-y))
                 thing1
   (with-accessors ((x2 thing-x) (y2 thing-y))
                   thing2
     (list (list x1 (thing-x thing1) y1 (thing-y thing1)
                 x2 (thing-x thing2) y2 (thing-y thing2))
           (setq x1 (+ y1 x2))
           (list x1 (thing-x thing1) y1 (thing-y thing1)
                 x2 (thing-x thing2) y2 (thing-y thing2))
           (setf (thing-x thing2) (list x1))
           (list x1 (thing-x thing1) y1 (thing-y thing1)
                 x2 (thing-x thing2) y2 (thing-y thing2)))))
@t{ |> } Changing X from 1 to 9 in #<THING 43135676>.
@t{ |> } Changing X from 7 to (9) in #<THING 43147374>.
@result{}  ((1 1 2 2 7 7 8 8)
     9
     (9 9 2 2 7 7 8 8) 
     (9)
     (9 9 2 2 (9) (9) 8 8))
@end example

@subsubheading  Affected By::

@b{defclass}

@subsubheading  Exceptional Situations::

The consequences are undefined if any @i{accessor-name} is not the name
of an accessor for the @i{instance}.

@subsubheading  See Also::

@ref{with-slots}
,
@ref{symbol-macrolet}

@subsubheading  Notes::

A @b{with-accessors} expression of the form:

@example

@w{@t{(with-accessors} (@r{slot-entry}_1 ...@r{slot-entry}_n) @i{instance-form} @r{form}_1 ...@r{form}_k)}@*
@end example

@noindent
expands into the equivalent of

@example

@w{@t{(}@t{let ((}in @i{instance-form}@t{))}}@*
@w{ @t{(symbol-macrolet (}@r{Q}_1... @r{Q}_n@t{)} @r{form}_1 ...@r{form}_k@t{))}}@*
@end example

@noindent
where @r{Q}_i is 

@example
@t{(}@r{variable-name}_i () 
@t{(@r{accessor-name}_i in))}
@end example


@node with-slots, defclass, with-accessors, Objects Dictionary
@subsection with-slots                                                          [Macro]

@code{with-slots}  @i{@r{(}@{@i{slot-entry}@}*@r{)}
		          instance-form 
                          @{@i{declaration}@}* @{@i{form}@}*}@*
   @result{}  @i{@{@i{result}@}*}

@w{@i{slot-entry} ::=slot-name | @r{(}variable-name slot-name@r{)}}

@subsubheading  Arguments and Values::

@i{slot-name}---a @i{slot} @i{name}; not evaluated.

@i{variable-name}---a @i{variable name}; not evaluated.

@i{instance-form}---a @i{form}; evaluted to produce @i{instance}.

@i{instance}---an @i{object}.

@i{declaration}---a @b{declare} @i{expression}; not evaluated.

@i{forms}---an @i{implicit progn}.

@i{results}---the @i{values} returned by the @i{forms}.

@subsubheading  Description::

The macro @b{with-slots} @i{establishes} a
@i{lexical environment}
for referring to the @i{slots} in the @i{instance} 
named by the given @i{slot-names} 
as though they were @i{variables}.  Within such a context
the value of the @i{slot} can be specified by using its slot name, as if
it were a lexically bound variable.  Both @b{setf} and @b{setq}
can be used to set the value of the @i{slot}.

The macro @b{with-slots} translates an appearance of the slot 
name as a @i{variable} into a call to @b{slot-value}.

@subsubheading  Examples::

@example
 (defclass thing ()
           ((x :initarg :x :accessor thing-x)
            (y :initarg :y :accessor thing-y)))
@result{}  #<STANDARD-CLASS THING 250020173>
 (defmethod (setf thing-x) :before (new-x (thing thing))
   (format t "~&Changing X from ~D to ~D in ~S.~
           (thing-x thing) new-x thing))
 (setq thing (make-instance 'thing :x 0 :y 1)) @result{}  #<THING 62310540>
 (with-slots (x y) thing (incf x) (incf y)) @result{}  2
 (values (thing-x thing) (thing-y thing)) @result{}  1, 2
 (setq thing1 (make-instance 'thing :x 1 :y 2)) @result{}  #<THING 43135676>
 (setq thing2 (make-instance 'thing :x 7 :y 8)) @result{}  #<THING 43147374>
 (with-slots ((x1 x) (y1 y))
             thing1
   (with-slots ((x2 x) (y2 y))
               thing2
     (list (list x1 (thing-x thing1) y1 (thing-y thing1)
                 x2 (thing-x thing2) y2 (thing-y thing2))
           (setq x1 (+ y1 x2))
           (list x1 (thing-x thing1) y1 (thing-y thing1)
                 x2 (thing-x thing2) y2 (thing-y thing2))
           (setf (thing-x thing2) (list x1))
           (list x1 (thing-x thing1) y1 (thing-y thing1)
                 x2 (thing-x thing2) y2 (thing-y thing2)))))
@t{ |> } Changing X from 7 to (9) in #<THING 43147374>.
@result{}  ((1 1 2 2 7 7 8 8)
     9
     (9 9 2 2 7 7 8 8) 
     (9)
     (9 9 2 2 (9) (9) 8 8))
@end example

@subsubheading  Affected By::

@b{defclass}

@subsubheading  Exceptional Situations::

The consequences are undefined if any @i{slot-name} is not the name
of a @i{slot} in the @i{instance}.

@subsubheading  See Also::

@ref{with-accessors}
,
@ref{slot-value}
,
@ref{symbol-macrolet}

@subsubheading  Notes::

A @b{with-slots} expression of the form:

@example

@w{@t{(with-slots} (@r{slot-entry}_1 ...@r{slot-entry}_n) @i{instance-form} @r{form}_1 ...@r{form}_k)}@*
@end example

@noindent
expands into the equivalent of

@example

@w{@t{(}@t{let ((}in @i{instance-form}@t{))}}@*
@w{ @t{(symbol-macrolet (}@r{Q}_1... @r{Q}_n@t{)} @r{form}_1 ...@r{form}_k@t{))}}@*
@end example

@noindent
where @r{Q}_i is 

@example
@t{(}@r{slot-entry}_i () 
@t{(slot-value }in '@r{slot-entry}_i@t{))}
@end example

@noindent
if @r{slot-entry}_i is a @i{symbol}
and is

@example
@t{(}@r{variable-name}_i () 
@t{(slot-value }in '@r{slot-name}_i@t{))}
@end example


@noindent
if @r{slot-entry}_i
is of the form 

@example
@t{(}@r{variable-name}_i 
@r{slot-name}_i@t{)}
@end example

@node defclass, defgeneric, with-slots, Objects Dictionary
@subsection defclass                                                            [Macro]

@code{defclass}  @i{@i{class-name} @r{(}@{@i{superclass-name}@}*@r{)}
@r{(}@{@i{slot-specifier}@}*@r{)}
 [[!@i{class-option}]]}@*
   @result{}  @i{new-class}

@w{ slot-specifier::=@i{slot-name} | (@i{slot-name} [[!@i{slot-option}]])}@*

@w{ @i{slot-name}::= @i{symbol}}@*

@w{ slot-option::=@{@t{:reader} @i{reader-function-name}@}* |}@*
@w{                         @{@t{:writer} @i{writer-function-name}@}* |}@*
@w{                         @{@t{:accessor} @i{reader-function-name}@}* |}@*
@w{                         @{@t{:allocation} @i{allocation-type}@} |}@*
@w{                         @{@t{:initarg} @i{initarg-name}@}* |}@*
@w{                         @{@t{:initform} @i{form}@} |}@*
@w{                         @{@t{:type} @i{type-specifier}@} |}@*
@w{                         @{@t{:documentation} @i{string}@}}@*

@w{ @i{function-name}::= @{@i{symbol} | @t{(setf @i{symbol})}@}}@*

@w{ class-option::=(@t{:default-initargs} @t{.} @i{initarg-list}) |}@*
@w{                          (@t{:documentation} @i{string}) |}@*
@w{                          (@t{:metaclass} @i{class-name})}@*

@subsubheading  Arguments and Values::

@i{Class-name}---a @i{non-nil} @i{symbol}.

@i{Superclass-name}--a @i{non-nil} @i{symbol}.

@i{Slot-name}--a @i{symbol}.
  The @i{slot-name} argument is 
  a @i{symbol} that is syntactically valid for use as a variable name.

@i{Reader-function-name}---a @i{non-nil} @i{symbol}.
 @t{:reader} can be supplied more than once for a given @i{slot}.

@i{Writer-function-name}---a @i{generic function} name.
 @t{:writer} can be supplied more than once for a given @i{slot}.

@i{Reader-function-name}---a @i{non-nil} @i{symbol}.
 @t{:accessor} can be supplied more than once for a given @i{slot}.

@i{Allocation-type}---(member @t{:instance} @t{:class}).
 @t{:allocation} can be supplied once at most for a given @i{slot}.

@i{Initarg-name}---a @i{symbol}.
 @t{:initarg} can be supplied more than once for a given @i{slot}.  

@i{Form}---a @i{form}. 
 @t{:init-form} can be supplied once at most for a given @i{slot}.  

@i{Type-specifier}---a @i{type specifier}.
 @t{:type} can be supplied once at most for a given @i{slot}. 

@i{Class-option}--- refers to the @i{class} as a whole or to all class @i{slots}.

@i{Initarg-list}---a @i{list} of alternating initialization argument
		        @i{names} and default initial value @i{forms}.
 @t{:default-initargs} can be supplied at most once.

@i{Class-name}---a @i{non-nil} @i{symbol}.
 @t{:metaclass} can be supplied once at most.

@i{new-class}---the new @i{class} @i{object}.

@subsubheading  Description::

The macro @b{defclass} defines a new named @i{class}.  It returns
the new @i{class} @i{object} as its result.

The syntax of @b{defclass} provides options for specifying
initialization arguments for @i{slots}, for specifying default
initialization values for @i{slots}, and for requesting that 
@i{methods} on specified @i{generic functions} be automatically 
generated for reading and writing the values of @i{slots}.  
No reader or writer functions are defined by default; 
their generation must be explicitly requested.  However,
@i{slots} can always be @i{accessed} using @b{slot-value}.

Defining a new @i{class} also causes a @i{type} of the same name to be
defined.  The predicate @t{(typep @i{object} @i{class-name})} returns
true if the @i{class} of the given @i{object} is 
the @i{class} named by @i{class-name} itself or
a subclass of the class @i{class-name}.  A @i{class} @i{object} 
can be used as a @i{type specifier}.  
Thus @t{(typep @i{object} @i{class})} returns @i{true}
if the @i{class} of the @i{object} is 
@i{class} itself or a subclass of @i{class}.   

The @i{class-name} argument specifies the @i{proper name} 
of the new @i{class}.  
If a @i{class} with the same @i{proper name} already exists 
 and that @i{class} is an @i{instance} of @b{standard-class}, 
 and if the @b{defclass} form for the definition of the new @i{class}
      specifies a @i{class} of @i{class} @b{standard-class},
the existing @i{class} is redefined,
and instances of it (and its @i{subclasses}) are updated 
 to the new definition at the time that they are next @i{accessed}.
For details, see @ref{Redefining Classes}.

Each @i{superclass-name} argument 
specifies a direct @i{superclass} of the new @i{class}.  
If the @i{superclass} list is empty, then the @i{superclass}
defaults depending on the @i{metaclass}, 
with @b{standard-object} being the
default for @b{standard-class}.

The new @i{class} will
inherit @i{slots} and @i{methods} 
from each of its direct @i{superclasses}, from
their direct @i{superclasses}, and so on.  
For a discussion of how @i{slots} and @i{methods} are inherited,
see @ref{Inheritance}.

The following slot options are available:

@table @asis

@item @t{*}  
The @t{:reader} slot option specifies that an @i{unqualified method} is
to be defined on the @i{generic function} named @i{reader-function-name}
to read the value of the given @i{slot}.

@item @t{*}  
The @t{:writer} slot option specifies that an @i{unqualified method} is
to be defined on the @i{generic function} named @i{writer-function-name}
to write the value of the @i{slot}.

@item @t{*}  
The @t{:accessor} slot option specifies that an @i{unqualified method}
is to be defined on the generic function named @i{reader-function-name}
to read the value of the given @i{slot}
and that an @i{unqualified method} is to be defined on the 
@i{generic function} named @t{(setf @i{reader-function-name})} to be
used with @b{setf} to modify the value of the @i{slot}.

@item @t{*}  
The @t{:allocation} slot option is used to specify where storage is
to be allocated for the given @i{slot}.  Storage for a 
@i{slot} can be located
in each instance or in the @i{class} @i{object} itself.
The value of the @i{allocation-type} argument can be 
either the keyword @t{:instance}
or the keyword @t{:class}.    If the @t{:allocation}
slot option is not specified, the effect is the same as specifying
@t{:allocation :instance}.
@table @asis

@item --  
If @i{allocation-type} is @t{:instance}, a @i{local slot} of
the name @i{slot-name} is allocated in each instance of the 
@i{class}.

@item --  
If @i{allocation-type} is @t{:class}, a shared 
@i{slot} of the given                                      
name is allocated in the @i{class} @i{object} created by this @b{defclass}
form.  The value of the @i{slot} is shared by all 
@i{instances} of the @i{class}.
If a class C_1 defines such a @i{shared slot}, any 
subclass C_2 of
C_1 will share this single @i{slot} unless the @b{defclass} form
for C_2 specifies a @i{slot} of the same @i{name} or there is a
superclass of C_2 that precedes C_1 in the class precedence
list of C_2 and that defines a @i{slot} of the same @i{name}.
@end table

@item @t{*}  
The @t{:initform} slot option is used to provide a default
initial value form to be used in the initialization of the @i{slot}.  This
@i{form} is evaluated every time it is used to initialize the 
@i{slot}.  The
lexical environment in which this @i{form} is evaluated is the lexical
environment in which the @b{defclass} form was evaluated.
Note that the lexical environment refers both to variables and to
functions.  For @i{local slots}, the dynamic environment is the dynamic
environment in which @b{make-instance} is called; for shared
@i{slots}, the dynamic environment is the dynamic environment in which the
@b{defclass} form was evaluated.  
See @ref{Object Creation and Initialization}.

No implementation is permitted to extend the syntax of @b{defclass}
to allow @t{(@i{slot-name} @i{form})} as an abbreviation for 
@t{(@i{slot-name} :initform @i{form})}.

[Reviewer Note by Barmar: Can you extend this to mean something else?]

@item @t{*}  
The @t{:initarg} slot option declares an initialization
argument named @i{initarg-name} and specifies that this
initialization argument initializes the given @i{slot}.  If the
initialization argument has a value in the call to
@b{initialize-instance}, the value will be stored into the given @i{slot},
and the slot's @t{:initform} slot option, if any, is not
evaluated.  If none of the initialization arguments specified for a
given @i{slot} has a value, the @i{slot} is initialized according to the
@t{:initform} slot option, if specified.  

@item @t{*}  
The @t{:type} slot option specifies that the contents of the
@i{slot} will always be of the specified data type.  It effectively
declares the result type of the reader generic function when applied
to an @i{object} of this @i{class}.  The consequences of attempting to store in a
@i{slot} a value that does not satisfy the type of the @i{slot} are undefined.
The @t{:type} slot option is further discussed in 
@ref{Inheritance of Slots and Slot Options}.

@item @t{*}  
The @t{:documentation} slot option provides a @i{documentation string}
for the @i{slot}.  @t{:documentation} can be supplied once at most 
for a given @i{slot}. 
[Reviewer Note by Barmar: How is this retrieved?]
@end table

Each class option is an option that refers to the @i{class} as a whole.
The following class options are available:

@table @asis

@item @t{*}  
The @t{:default-initargs} class option is followed by a list of
alternating initialization argument @i{names} and default initial value
forms.  If any of these initialization arguments does not appear in
the initialization argument list supplied to @b{make-instance}, the
corresponding default initial value form is evaluated, and the
initialization argument @i{name} and the @i{form}'s value are added to the end
of the initialization argument list before the instance is created;
see @ref{Object Creation and Initialization}.
The default initial value form is evaluated each time it is used.  The lexical
environment in which this @i{form} is evaluated is the lexical environment
in which the @b{defclass} form was evaluated.  The dynamic
environment is the dynamic environment in which @b{make-instance}
was called.  If an initialization argument @i{name} appears more than once
in a @t{:default-initargs} class option, an error is signaled.

@item @t{*}  

The @t{:documentation} class option causes a @i{documentation string} 
to be attached with the @i{class} @i{object},
and attached with kind @b{type} to the @i{class-name}.
@t{:documentation} can be supplied once at most.

@item @t{*}  
The @t{:metaclass} class option is used to specify that
instances of the @i{class} being defined are to have a different metaclass
than the default provided by the system (the @i{class} @b{standard-class}).

@end table

Note the following rules of @b{defclass} for @i{standard classes}:

@table @asis

@item @t{*}  
It is not required that the @i{superclasses} of a @i{class} be defined before
the @b{defclass} form for that @i{class} is evaluated.

@item @t{*}  
All the @i{superclasses} of a @i{class} must be defined before 
an @i{instance} of the @i{class} can be made.

@item @t{*}  
A @i{class} must be defined before it can be used as a parameter
specializer in a @b{defmethod} form.

@end table

The object system can be extended to cover situations where these rules are not
obeyed.

Some slot options are inherited by a @i{class} from its 
@i{superclasses}, and
some can be shadowed or altered by providing a local slot description.
No class options except @t{:default-initargs} are inherited.  For a
detailed description of how @i{slots} and slot options are inherited, 
see @ref{Inheritance of Slots and Slot Options}.

The options to @b{defclass} can be extended.  It is required that
all implementations signal an error if they observe a class option or
a slot option that is not implemented locally.

It is valid to specify more than one reader, writer, accessor, or
initialization argument for a @i{slot}.  No other slot option can
appear
more than once in a single slot description, or an error is
signaled.

If no reader, writer, or accessor is specified for a @i{slot}, 
the @i{slot} can only be @i{accessed} by the @i{function} @b{slot-value}.

If a @b{defclass} @i{form} appears as a @i{top level form},
the @i{compiler} must make the @i{class} @i{name} be recognized as a
valid @i{type} @i{name} in subsequent declarations (as for @b{deftype})
and be recognized as a valid @i{class} @i{name} for @b{defmethod}
@i{parameter specializers} and for use as the @t{:metaclass} option of a
subsequent @b{defclass}.  The @i{compiler} must make 
the @i{class} definition 
available to be returned by @b{find-class} when its @i{environment}
@i{argument} is a value received as the @i{environment parameter} of a @i{macro}.

@subsubheading  Exceptional Situations::

If there are any duplicate slot names, 
an error of @i{type} @b{program-error} is signaled.

If an initialization argument @i{name} appears more than once in 
@t{:default-initargs} class option, 
an error of @i{type} @b{program-error} is signaled.

If any of the following slot options appears more than once in a
single slot description, an error of @i{type} @b{program-error}
is signaled: @t{:allocation},
@t{:initform}, @t{:type}, @t{:documentation}.

It is required that all implementations signal 
an error of @i{type} @b{program-error} if they observe a class option 
or a slot option that is not implemented locally.

@subsubheading  See Also::

@ref{documentation}
,
@ref{Initialize-Instance}
,
@ref{make-instance}
,
@ref{slot-value}
,
@ref{Classes},
@ref{Inheritance},
@ref{Redefining Classes},
@ref{Determining the Class Precedence List},
@ref{Object Creation and Initialization}

@node defgeneric, defmethod, defclass, Objects Dictionary
@subsection defgeneric                                                          [Macro]

@code{defgeneric}  @i{function-name gf-lambda-list
	  		  [[!@i{option} | @{!@i{method-description}@}*]]}@*
   @result{}  @i{new-generic}

@w{@i{option} ::=@r{(}@t{:argument-precedence-order} @{@i{parameter-name}@}^+@r{)} |}
@w{           @r{(}@b{declare} @{@i{gf-declaration}@}^+@r{)} |}
@w{           @r{(}@t{:documentation} @i{gf-documentation}@r{)} |}
@w{           @r{(}@t{:method-combination} @i{method-combination} @{@i{method-combination-argument}@}*@r{)} |}
@w{           @r{(}@t{:generic-function-class} @i{generic-function-class}@r{)} |}
@w{           @r{(}@t{:method-class} @i{method-class}@r{)}}

@w{@i{method-description} ::=@r{(}@t{:method} @{@i{method-qualifier}@}* @i{specialized-lambda-list} @r{[[@{@i{declaration}@}* | @i{documentation}]]} @{@i{form}@}*@r{)}}

@subsubheading  Arguments and Values::

@i{function-name}---a @i{function name}.

@i{generic-function-class}---a @i{non-nil} @i{symbol} naming a @i{class}.

@i{gf-declaration}---an @b{optimize} @i{declaration specifier};
  other @i{declaration specifiers} are not permitted.

@i{gf-documentation}---a @i{string}; not evaluated.

@i{gf-lambda-list}---a @i{generic function lambda list}.

@i{method-class}---a @i{non-nil} @i{symbol} naming a @i{class}.

@i{method-combination-argument}---an @i{object.}

@i{method-combination-name}---a @i{symbol} 
				  naming a @i{method combination} @i{type}.

@i{method-qualifiers},
@i{specialized-lambda-list},
@i{declarations},
@i{documentation},
@i{forms}---as per @b{defmethod}.

@i{new-generic}---the @i{generic function} @i{object}.

@i{parameter-name}---a @i{symbol} that names a @i{required parameter} 
			 in the @i{lambda-list}.
  (If the @t{:argument-precedence-order} option is specified,
   each @i{required parameter} in the @i{lambda-list}
   must be used exactly once as a @i{parameter-name}.)

@subsubheading  Description::

The macro @b{defgeneric} is used to define a @i{generic function}
or to specify options and declarations that pertain 
to a @i{generic function} as a whole.

If @i{function-name} is a 
@i{list} it must be of the form @t{(setf @i{symbol})}.
If @t{(fboundp @i{function-name})} is @i{false}, a new
@i{generic function} is created.  

If @t{(fdefinition @i{function-name})} is a @i{generic function}, that 

@i{generic function}
is modified.  If @i{function-name} names 
an @i{ordinary function},
a @i{macro}, or a @i{special operator}, 
an error is signaled.

The effect of the @b{defgeneric} macro is as if the following three
steps were performed: first, 
@i{methods} defined by previous @b{defgeneric} @i{forms} are removed; 

[Reviewer Note by Barmar: Shouldn't this (second) be first?]
second, @b{ensure-generic-function}
is called; and finally, @i{methods} specified by the current
@b{defgeneric} @i{form} are added to the @i{generic function}. 

Each @i{method-description} defines a @i{method} on the @i{generic function}.
The @i{lambda list} of each @i{method} must be congruent with the 
@i{lambda list}
specified by the @i{gf-lambda-list} option.  
If no @i{method} descriptions are specified and a @i{generic function} of the same
name does not already exist, a @i{generic function} with no 
@i{methods} is created.

The @i{gf-lambda-list} argument of @b{defgeneric} specifies the shape of
@i{lambda lists} for the @i{methods} on this @i{generic function}.
All @i{methods} on the resulting 
@i{generic function} must have
@i{lambda lists} that are congruent with this shape.  If a @b{defgeneric}
form is evaluated and some 
@i{methods} for that @i{generic function}
have @i{lambda lists} that are not congruent with that given in
the @b{defgeneric} form, an error is signaled.  For further details
on method congruence, see @ref{Congruent Lambda-lists for all Methods of a Generic Function}.

The @i{generic function} passes to the 
@i{method} all the argument values passed to
it, and only those; default values are not supported.
Note that optional and keyword arguments in method definitions, however,
can have default initial value forms and can use supplied-p parameters. 

The following options are provided.  

Except as otherwise noted, 

a given option may occur only once.

@table @asis

@item @t{*}  
The @t{:argument-precedence-order} option is used to specify the
order in which the required arguments in a call to the @i{generic function}
are tested for specificity when selecting a particular
@i{method}. Each required argument, as specified in the @i{gf-lambda-list}
argument, must be included exactly once as a @i{parameter-name}
so that the full and unambiguous precedence order is
supplied.  If this condition is not met, an error is signaled.

[Reviewer Note by Barmar: What is the default order?]

@item @t{*}  
The @b{declare} option is used to specify declarations that pertain
to the @i{generic function}.

An @b{optimize} @i{declaration specifier} is allowed.
It specifies whether method selection should be optimized for 
speed or space, but it has no effect on @i{methods}.
To control how a @i{method} is optimized, an @b{optimize}
declaration must be placed directly in the @b{defmethod} @i{form}
or method description.  The optimization qualities @b{speed} and
@b{space} are the only qualities this standard requires, but an
implementation can extend the object system to recognize other qualities.  
A simple implementation that has only one method selection technique 
and ignores @b{optimize} @i{declaration specifiers} is valid.

The @b{special}, @b{ftype}, @b{function}, @b{inline},
@b{notinline}, and @b{declaration} declarations are not permitted.
Individual implementations can extend the @b{declare} option to
support additional declarations.

[Editorial Note by KMP: Does ``additional'' mean including special, ftype, etc.?  
Or only other things that are not mentioned here?]
If an implementation notices a @i{declaration specifier} that it does
not support and that has not been proclaimed as a non-standard 
@i{declaration identifier} name in a @b{declaration} @i{proclamation}, 
it should issue a warning. 
[Editorial Note by KMP: The wording of this previous sentence,
particularly the word ``and'' suggests to me that you can `proclaim declaration'
of an unsupported declaration (e.g., ftype) in order to suppress the warning.
That seems wrong.  Perhaps it instead means to say ``does not support or 
is both undefined and not proclaimed declaration.'']

The @b{declare} option may be specified more than once.
The effect is the same as if the lists of @i{declaration specifiers} 
had been appended together into a single list and specified as a 
single @b{declare} option.

@item @t{*}  
The @t{:documentation} argument is a @i{documentation string}
to be attached to the @i{generic function} @i{object}, 
and to be attached with kind @b{function} to the @i{function-name}.

@item @t{*}  
The @t{:generic-function-class} option may be used to specify that
the @i{generic function} is to have a different @i{class} than
the default provided by the system (the @i{class} @b{standard-generic-function}).
The @i{class-name} argument is the name of a @i{class} that can be the
@i{class} of a @i{generic function}.  If @i{function-name} specifies
an existing @i{generic function} that has a different value for the
@t{:generic-function-class} argument and the new generic function 
@i{class} is compatible with the old, @b{change-class} is called 
to change the @i{class} of the @i{generic function}; 
otherwise an error is signaled.

@item @t{*}  
The @t{:method-class} option is used to specify that all @i{methods} on
this @i{generic function} are to have a different @i{class} from the 
default provided by the system (the @i{class} @b{standard-method}).
The @i{class-name} argument is the name of a @i{class} that is capable 
of being the @i{class} of a @i{method}.

[Reviewer Note by Barmar: Is @b{change-class} called on existing methods?]

@item @t{*}  
The @t{:method-combination} option is followed by a symbol that
names a type of method combination.  The arguments (if any) that
follow that symbol depend on the type of method combination.  Note
that the standard method combination type does not support any
arguments.  However, all types of method combination defined by the
short form of @b{define-method-combination} accept an optional
argument named @i{order}, defaulting to @t{:most-specific-first},
where a value of @t{:most-specific-last} reverses
the order of the primary @i{methods} without affecting the order of the
auxiliary @i{methods}.

@end table

The @i{method-description} arguments define @i{methods} that will
be associated with the @i{generic function}.  The @i{method-qualifier}
and @i{specialized-lambda-list} arguments in a method description
are the same as for @b{defmethod}.

The @i{form} arguments specify the method body.  The body of the
@i{method} is enclosed in an @i{implicit block}.
If @i{function-name} is a @i{symbol}, this block bears the same name as
the @i{generic function}.  If @i{function-name} is a 
@i{list} of the
form @t{(setf @i{symbol})}, the name of the block is @i{symbol}.  

Implementations can extend @b{defgeneric} to include other options.
It is required that an implementation signal an error if
it observes an option that is not implemented locally.

@b{defgeneric} is not required to perform any compile-time side effects.
In particular, the @i{methods} are not installed for invocation during 
compilation.  An @i{implementation} may choose to store information about
the @i{generic function} for the purposes of compile-time error-checking
(such as checking the number of arguments on calls, or noting that a definition
 for the function name has been seen).

@subsubheading  Examples::

@subsubheading  Exceptional Situations::

If @i{function-name} names an @i{ordinary function}, a @i{macro},
or a @i{special operator}, an error of @i{type} @b{program-error} is signaled.

Each required argument, as specified in the @i{gf-lambda-list}
argument, must be included exactly once as a @i{parameter-name},
or an error of @i{type} @b{program-error} is signaled.

The @i{lambda list} of each @i{method} specified by a 
@i{method-description} must be congruent with the @i{lambda list} specified
by the @i{gf-lambda-list} option, or
an error of @i{type} @b{error} is signaled.

If a @b{defgeneric} form is evaluated and some @i{methods} for
that @i{generic function} have @i{lambda lists} that are not congruent with
that given in the @b{defgeneric} form, 
an error of @i{type} @b{error} is signaled.

A given @i{option} may occur only once,
or an error of @i{type} @b{program-error} is signaled.

[Reviewer Note by Barmar: This says that an error is signaled if you specify the same generic
    function class as it already has!]
If @i{function-name} specifies an existing @i{generic function} 
that has a different value for the @t{:generic-function-class} 
argument and the new generic function @i{class} is compatible with the
old, @b{change-class} is called to change the @i{class} of 
the @i{generic function}; otherwise an error of @i{type} @b{error} is signaled.

Implementations can extend @b{defgeneric} to include other options.
It is required that an implementation 
signal an error of @i{type} @b{program-error} if
it observes an option that is not implemented locally.

@subsubheading  See Also::

@ref{defmethod}
,
@ref{documentation}
,
@ref{ensure-generic-function}
,

@b{generic-function},

@ref{Congruent Lambda-lists for all Methods of a Generic Function}

@node defmethod, find-class, defgeneric, Objects Dictionary
@subsection defmethod                                                           [Macro]

@code{defmethod}  @i{@i{function-name}
				      @{@i{method-qualifier}@}*
				      @i{specialized-lambda-list}
				@r{[[@{@i{declaration}@}* | @i{documentation}]]} @{@i{form}@}*}@*
   @result{}  @i{new-method}

@i{function-name}::= @{@i{symbol} 
| @t{(setf @i{symbol})}@}

@i{method-qualifier}::= @i{non-list}

@w{ @i{specialized-lambda-list}::= (@{@i{var} | @r{(}@r{@i{var} @i{parameter-specializer-name}}@r{)}@}*}@*
@w{                             @t{[}@r{&optional} @{@i{var} | @r{(}var @t{[}@i{initform} @r{@r{[}@i{supplied-p-parameter}@r{]}} @t{]}@r{)}@}*@t{]}}@*
@w{                             @t{[}@t{&rest} @i{var}@t{]}}@*
@w{                             @t{[}@r{@r{&key}}@{@i{var} | @r{(}@{@i{var} | @r{(}@i{keyword}@i{var}@r{)}@} @t{[}@i{initform} @r{[}@i{supplied-p-parameter}@r{]} @t{]}@r{)}@}*}@*
@w{                                          @r{[}@b{&allow-other-keys}@r{]} @t{]}}@*
@w{                             @t{[}@t{&aux} @{@i{var} | @r{(}@i{var} @r{[}@i{initform}@r{]} @r{)}@}*@t{]} @r{)}}@*

@w{ @i{parameter-specializer-name}::= @i{symbol} | @r{(}@t{eql} @i{eql-specializer-form}@r{)}}@*

@subsubheading  Arguments and Values::

@i{declaration}---a @b{declare} @i{expression}; not evaluated.

@i{documentation}---a @i{string}; not evaluated.

@i{var}---a @i{variable} @i{name}.

@i{eql-specializer-form}---a @i{form}.

@i{Form}---a @i{form}.

@i{Initform}---a @i{form}.

@i{Supplied-p-parameter}---variable name. 

@i{new-method}---the new @i{method} @i{object}.

@subsubheading  Description::

The macro @b{defmethod} defines a @i{method} on a 
@i{generic function}.  

If @t{(fboundp @i{function-name})} is @b{nil}, a 
@i{generic function} is created with default values for 
the argument precedence order
(each argument is more specific than the arguments to its right
in the argument list),
for the generic function class (the @i{class} @b{standard-generic-function}),
for the method class (the @i{class} @b{standard-method}),
and for the method combination type (the standard method combination type).
The @i{lambda list} of the @i{generic function} is
congruent with the @i{lambda list} of the 
@i{method} being defined; if the
@b{defmethod} form mentions keyword arguments, the @i{lambda list} of
the @i{generic function} 
will mention @t{&key} (but no keyword
arguments).  If @i{function-name} names 
an @i{ordinary function},
a @i{macro}, or a @i{special operator}, 
an error is signaled.

If a @i{generic function} is currently named by @i{function-name},
the @i{lambda list} of the
@i{method} must be congruent with the @i{lambda list} of the 
@i{generic function}.
If this condition does not hold, an error is signaled.  
For a definition of congruence in this context, see @ref{Congruent Lambda-lists for all Methods of a Generic Function}.

Each @i{method-qualifier} argument is an @i{object} that is used by
method combination to identify the given @i{method}.  
The method combination type might further
restrict what a method @i{qualifier} can be.
The standard method combination type allows for @i{unqualified methods} and
@i{methods} whose sole
@i{qualifier} is one of the keywords @t{:before}, @t{:after}, or @t{:around}.

The @i{specialized-lambda-list} argument is like an ordinary
@i{lambda list} except that the @i{names} of required parameters can
be replaced by specialized parameters.  A specialized parameter is a
list of the form 
@t{(@i{var} @i{parameter-specializer-name})}.
Only required parameters can be
specialized.  If @i{parameter-specializer-name} is a @i{symbol} it names a
@i{class}; if it is a @i{list},
it is of the form @t{(eql @i{eql-specializer-form})}.  The parameter
specializer name @t{(eql @i{eql-specializer-form})} indicates
that the corresponding argument must be @b{eql} to the @i{object} that
is the value of @i{eql-specializer-form} for the @i{method} to be applicable.  
The @i{eql-specializer-form} is evaluated at the time
that the expansion of the @b{defmethod} macro is evaluated.  
If no @i{parameter specializer name} is specified for a given
required parameter, the @i{parameter specializer} defaults to 
the @i{class} @b{t}.
For further discussion, see @ref{Introduction to Methods}.

The @i{form} arguments specify the method body.
The body of the @i{method} is enclosed in an @i{implicit block}.  If
@i{function-name} is a @i{symbol}, 
this block bears the same @i{name} as the @i{generic function}.  
If @i{function-name} is a @i{list} of the form 
@t{(setf @i{symbol})}, the @i{name} of the block is @i{symbol}.  

The @i{class} of the @i{method} @i{object} that is created is that given by the 
method class option of the @i{generic function} 
on which the @i{method} is defined.

If the @i{generic function} already has a @i{method} that agrees with the
@i{method} being defined on @i{parameter specializers} and @i{qualifiers},
@b{defmethod} replaces the existing @i{method} with the one now being
defined.
For a definition of agreement in this context.
see @ref{Agreement on Parameter Specializers and Qualifiers}.

The @i{parameter specializers} are derived from 
the @i{parameter specializer names} as described in
@ref{Introduction to Methods}.

The expansion of the @b{defmethod} macro ``refers to'' each
specialized parameter (see the description of @b{ignore} 
within the description of @b{declare}).
This includes parameters that
have an explicit @i{parameter specializer name} of @b{t}.  This means
that a compiler warning does not occur if the body of the @i{method} does
not refer to a specialized parameter, while a warning might occur
if the body of the @i{method} does not refer to an unspecialized parameter.
For this reason, a parameter that specializes on @b{t} is not quite synonymous
with an unspecialized parameter in this context.

Declarations at the head of the method body that apply to the 
method's @i{lambda variables} are treated as @i{bound declarations}
whose @i{scope} is the same as the corresponding @i{bindings}.

Declarations at the head of the method body that apply to the 
functional bindings of @b{call-next-method} or @b{next-method-p}
apply to references to those functions within the method body @i{forms}.
Any outer @i{bindings} of the @i{function names} @b{call-next-method} and
@b{next-method-p}, and declarations associated with such @i{bindings}
are @i{shadowed}_2 within the method body @i{forms}.

The @i{scope} of @i{free declarations} at the head of the method body 
is the entire method body, 
which includes any implicit local function definitions
  but excludes @i{initialization forms} for the @i{lambda variables}.

@b{defmethod} is not required to perform any compile-time side effects.
In particular, the @i{methods} are not installed for invocation during 
compilation.  An @i{implementation} may choose to store information about
the @i{generic function} for the purposes of compile-time error-checking
(such as checking the number of arguments on calls, or noting that a definition
 for the function name has been seen).

@i{Documentation} is attached as a @i{documentation string}
to the @i{method} @i{object}.

@subsubheading  Affected By::

The definition of the referenced @i{generic function}.

@subsubheading  Exceptional Situations::

If @i{function-name} names an @i{ordinary function},
a @i{macro}, or a @i{special operator}, 
an error of @i{type} @b{error} is signaled.

If a @i{generic function} is currently named by @i{function-name},
the @i{lambda list} of the
@i{method} must be congruent with the @i{lambda list} of the 
@i{generic function}, or
an error of @i{type} @b{error} is signaled.

@subsubheading  See Also::

@ref{defgeneric}
, 
@ref{documentation}
,
@ref{Introduction to Methods},
@ref{Congruent Lambda-lists for all Methods of a Generic Function},
@ref{Agreement on Parameter Specializers and Qualifiers},
@ref{Syntactic Interaction of Documentation Strings and Declarations}

@node find-class, next-method-p, defmethod, Objects Dictionary
@subsection find-class                                                       [Accessor]

@code{find-class}  @i{symbol @r{&optional} errorp environment} @result{}  @i{class}

(setf (@code{         find-class} @i{symbol @r{&optional} errorp environment}) new-class)@*

@subsubheading  Arguments and Values::

@i{symbol}---a @i{symbol}.

@i{errorp}---a @i{generalized boolean}.
 The default is @i{true}.

@i{environment} -- same as the @b{&environment} argument to
  macro expansion functions and is used to distinguish between 
  compile-time and run-time environments.

  The @b{&environment} argument has 
  @i{dynamic extent}; the consequences are undefined if 
  the @b{&environment} argument is 
  referred to outside the @i{dynamic extent} 
  of the macro expansion function.

@i{class}---a @i{class} @i{object}, or @b{nil}.

@subsubheading  Description::

Returns the @i{class} @i{object} named by the @i{symbol}
in the @i{environment}.  If there is no such @i{class},
@b{nil} is returned if @i{errorp} is @i{false}; otherwise,
if @i{errorp} is @i{true}, an error is signaled.

The @i{class} associated with a particular @i{symbol} can be changed by using
@b{setf} with @b{find-class};

or, if the new @i{class} given to @b{setf} is @b{nil},
the @i{class} association is removed 
(but the @i{class} @i{object} itself is not affected).

The results are undefined if the user attempts to change

or remove

the @i{class} associated with a 
@i{symbol} that is defined as a @i{type specifier} in this standard.
See @ref{Integrating Types and Classes}.

When using @b{setf} of @b{find-class}, any @i{errorp} argument is @i{evaluated}
for effect, but any @i{values} it returns are ignored; the @i{errorp}
@i{parameter} is permitted primarily so that the @i{environment} @i{parameter}
can be used.

The @i{environment} might be used to distinguish between a compile-time and a
run-time environment.

@subsubheading  Exceptional Situations::

If there is no such @i{class} and @i{errorp} is @i{true},
@b{find-class} signals an error of @i{type} @b{error}.

@subsubheading  See Also::

@ref{defmacro}
,
@ref{Integrating Types and Classes}

@node next-method-p, call-method, find-class, Objects Dictionary
@subsection next-method-p                                              [Local Function]

@subsubheading  Syntax::

@code{next-method-p}  @i{<@i{no @i{arguments}}>} @result{}  @i{generalized-boolean}

@subsubheading  Arguments and Values::

@i{generalized-boolean}---a @i{generalized boolean}. 

@subsubheading  Description::

The locally defined function @b{next-method-p} can be used 

within the body @i{forms} (but not the @i{lambda list})

defined by a @i{method-defining form} to determine
whether a next @i{method} exists.

The @i{function} @b{next-method-p} has @i{lexical scope} and @i{indefinite extent}.

Whether or not @b{next-method-p} is @i{fbound} in the
@i{global environment} is @i{implementation-dependent};
however, the restrictions on redefinition and @i{shadowing} of
@b{next-method-p} are the same as for @i{symbols} in the @t{COMMON-LISP} @i{package}
which are @i{fbound} in the @i{global environment}.
The consequences of attempting to use @b{next-method-p} outside
of a @i{method-defining form} are undefined.

@subsubheading  See Also::

@ref{call-next-method}
,
@ref{defmethod}
,
@ref{call-method}

@node call-method, call-next-method, next-method-p, Objects Dictionary
@subsection call-method, make-method                                      [Local Macro]

@subsubheading  Syntax::

@code{call-method}  @i{method @r{&optional} next-method-list} @result{}  @i{@{@i{result}@}*}

@code{make-method}  @i{form} @result{}  @i{method-object}

@subsubheading  Arguments and Values::

@i{method}---a @i{method} @i{object},
	      or a @i{list} (see below); not evaluated.

@i{method-object}---a @i{method} @i{object}.

@i{next-method-list}---a @i{list} of @i{method} @i{objects}; not evaluated.

@i{results}---the @i{values} returned by the @i{method} invocation.

@subsubheading  Description::

The macro @b{call-method} is used in method combination.  It hides
the @i{implementation-dependent} details of how 
@i{methods} are called. The
macro @b{call-method} has @i{lexical scope} and 
can only be used within
an @i{effective method} @i{form}.

[Editorial Note by KMP: This next paragraph still needs some work.]

Whether or not @b{call-method} is @i{fbound} in the
@i{global environment} is @i{implementation-dependent};
however, the restrictions on redefinition and @i{shadowing} of
@b{call-method} are the same as for @i{symbols} in the @t{COMMON-LISP} @i{package}
which are @i{fbound} in the @i{global environment}.
The consequences of attempting to use @b{call-method} outside
of an @i{effective method} @i{form} are undefined.

The macro @b{call-method} invokes the specified @i{method},
supplying it with arguments and with definitions for
@b{call-next-method} and for @b{next-method-p}.
If the invocation of @b{call-method} is lexically inside
of a @b{make-method}, the arguments are those that
were supplied to that method.  Otherwise the arguments are
those that were supplied to the generic function.
The definitions
of @b{call-next-method} and @b{next-method-p} rely on
the specified @i{next-method-list}.

If @i{method} is a @i{list}, the first element of the @i{list}
must be the symbol @b{make-method} and the second element must be
a @i{form}.  Such a @i{list} specifies a @i{method} @i{object}
whose @i{method} function has a body that is the given @i{form}.

@i{Next-method-list} can contain @i{method} @i{objects} or @i{lists},
the first element of which must be the symbol @b{make-method} and the
second element of which must be a @i{form}.

Those are the only two places where @b{make-method} can be used.
The @i{form} used with @b{make-method} is evaluated in
the @i{null lexical environment} augmented with a local macro definition
for @b{call-method} and with bindings named by
symbols not @i{accessible} from the @t{COMMON-LISP-USER} @i{package}.

The @b{call-next-method} function available to @i{method} 
will call the first @i{method} in @i{next-method-list}.
The @b{call-next-method} function
available in that @i{method}, in turn, will call the second
@i{method} in @i{next-method-list}, and so on, until
the list of next @i{methods} is exhausted.

If @i{next-method-list} is not supplied, the
@b{call-next-method} function available to
@i{method} signals an error of @i{type} @b{control-error}
and the @b{next-method-p} function
available to @i{method} returns @b{nil}.

@subsubheading  Examples::

@subsubheading  See Also:: 

@ref{call-next-method}
,
@ref{define-method-combination}
,
@ref{next-method-p}

@node call-next-method, compute-applicable-methods, call-method, Objects Dictionary
@subsection call-next-method                                           [Local Function]

@subsubheading  Syntax::

@code{call-next-method}  @i{@r{&rest} args} @result{}  @i{@{@i{result}@}*}

@subsubheading  Arguments and Values::

@i{arg}---an @i{object}.  

@i{results}---the @i{values} returned by the @i{method} it calls.

@subsubheading  Description::

The @i{function} @b{call-next-method} can be used 

within the body @i{forms} (but not the @i{lambda list})

of a @i{method} defined by a @i{method-defining form} to call the 
@i{next method}.

If there is no next @i{method}, the generic function 
@b{no-next-method} is called.

The type of method combination used determines which @i{methods}
can invoke @b{call-next-method}.  The standard 
@i{method combination} type allows @b{call-next-method} 
to be used within primary @i{methods} and @i{around methods}.
For generic functions using a type of method combination defined by
the short form of @b{define-method-combination},
@b{call-next-method} can be used in @i{around methods} only.

When @b{call-next-method} is called with no arguments, it passes the
current @i{method}'s original arguments to the next @i{method}.  Neither
argument defaulting, nor using @b{setq}, nor rebinding variables
with the same @i{names} as parameters of the @i{method} affects the values
@b{call-next-method} passes to the @i{method} it calls.

When @b{call-next-method} is called with arguments, the 
@i{next method} is called with those arguments.

If @b{call-next-method} is called with arguments but omits
optional arguments, the @i{next method} called defaults those arguments.

The @i{function} @b{call-next-method} returns any @i{values} that are
returned by the @i{next method}.

The @i{function} @b{call-next-method} has @i{lexical scope} and 
@i{indefinite extent} and can only be used within the body of a
@i{method} defined by a @i{method-defining form}.

Whether or not @b{call-next-method} is @i{fbound} in the
@i{global environment} is @i{implementation-dependent};
however, the restrictions on redefinition and @i{shadowing} of
@b{call-next-method} are the same as for @i{symbols} in the @t{COMMON-LISP} @i{package}
which are @i{fbound} in the @i{global environment}.
The consequences of attempting to use @b{call-next-method} outside
of a @i{method-defining form} are undefined.

@subsubheading  Affected By::

@b{defmethod}, @b{call-method}, @b{define-method-combination}.

@subsubheading  Exceptional Situations::

When providing arguments to @b{call-next-method}, 
the following rule must be satisfied or an error of @i{type} @b{error} 
should be
signaled: 
the ordered set of @i{applicable methods} for a changed set of arguments
for @b{call-next-method} must be the same as the ordered set of
@i{applicable methods} for the original arguments to the
@i{generic function}.
Optimizations of the error checking are possible, but they must not change
the semantics of @b{call-next-method}.

@subsubheading  See Also::

@ref{define-method-combination}
,
@ref{defmethod}
,
@ref{next-method-p}
,
@ref{no-next-method}
,
@ref{call-method}
,
@ref{Method Selection and Combination},
@ref{Standard Method Combination},
@ref{Built-in Method Combination Types}

@node compute-applicable-methods, define-method-combination, call-next-method, Objects Dictionary
@subsection compute-applicable-methods                      [Standard Generic Function]

@subsubheading  Syntax::

@code{compute-applicable-methods}  @i{generic-function function-arguments} @result{}  @i{methods}

@subsubheading  Method Signatures::

@code{compute-applicable-methods}  @i{@r{(}@i{generic-function} @b{standard-generic-function}@r{)}}

@subsubheading  Arguments and Values::

@i{generic-function}---a @i{generic function}.

@i{function-arguments}---a @i{list} of arguments for the @i{generic-function}.

@i{methods}---a @i{list} of @i{method} @i{objects}.

@subsubheading  Description::

Given a @i{generic-function} and a set of 
@i{function-arguments}, the function
@b{compute-applicable-methods} returns the set of @i{methods}
that are applicable for those arguments
sorted according to precedence order.
See @ref{Method Selection and Combination}.

@subsubheading  Affected By::

@b{defmethod}

@subsubheading  See Also::

@ref{Method Selection and Combination}

@node define-method-combination, find-method, compute-applicable-methods, Objects Dictionary
@subsection define-method-combination                                           [Macro]

@code{define-method-combination}  @i{name [[!@i{short-form-option}]]}@*
   @result{}  @i{name}

@code{define-method-combination}  @i{name lambda-list
                                @r{(}@{@i{method-group-specifier}@}*@r{)}
                                @r{[}@r{(}@t{:arguments} . args-lambda-list@r{)}@r{]}
                                @r{[}@r{(}@t{:generic-function} 
                                                   generic-function-symbol@r{)}@r{]}
                                [[@{@i{declaration}@}* | @i{documentation}]]
                                @{@i{form}@}*}@*
   @result{}  @i{name}

@w{@i{short-form-option} ::=@t{:documentation} @i{documentation} | }
@w{                      @t{:identity-with-one-argument} @i{identity-with-one-argument} |}
@w{                      @t{:operator} @i{operator}}

@w{@i{method-group-specifier} ::=@r{(}name @{@{@i{qualifier-pattern}@}^+ | predicate@} [[!@i{long-form-option}]]@r{)}}

@w{@i{long-form-option} ::=@t{:description} @i{description} |}
@w{                     @t{:order} @i{order} |}
@w{                     @t{:required} @i{required-p}}

@subsubheading  Arguments and Values::

@i{args-lambda-list}---
a @i{define-method-combination arguments lambda list}.

@i{declaration}---a @b{declare} @i{expression}; not evaluated.

@i{description}---a @i{format control}.

@i{documentation}---a @i{string}; not evaluated.

@i{forms}---an @i{implicit progn} 
  that must compute and return the @i{form} that specifies how
  the @i{methods} are combined, that is, the @i{effective method}.

@i{generic-function-symbol}---a @i{symbol}.

@i{identity-with-one-argument}---a @i{generalized boolean}.

@i{lambda-list}---@i{ordinary lambda list}.

@i{name}---a @i{symbol}. 
  Non-@i{keyword}, @i{non-nil} @i{symbols} are usually used.

@i{operator}---an @i{operator}.
  @i{Name} and @i{operator} are often the @i{same} @i{symbol}.
  This is the default, but it is not required.

@i{order}---@t{:most-specific-first} or @t{:most-specific-last}; evaluated.

@i{predicate}---a @i{symbol} that names a @i{function} of one argument
		    that returns a @i{generalized boolean}.

@i{qualifier-pattern}---a @i{list},
			 or the @i{symbol} @b{*}.

@i{required-p}---a @i{generalized boolean}.

@subsubheading  Description::

The macro @b{define-method-combination} is used to define new types
of method combination.

There are two forms of @b{define-method-combination}.  The short
form is a simple facility for the cases that are expected
to be most commonly needed.  The long form is more powerful but more
verbose.  It resembles @b{defmacro} in that the body is an
expression, usually using backquote, that computes a @i{form}.  Thus
arbitrary control structures can be implemented.  The long form also
allows arbitrary processing of method @i{qualifiers}.

@table @asis

@item @b{Short Form}  
The short form syntax of @b{define-method-combination} is recognized
when the second @i{subform} is a @i{non-nil} symbol or is not present.
When the short form is used, @i{name} is defined as a type of
method combination that produces a Lisp form
@t{(@r{@i{operator} @i{method-call} @i{method-call} ...})}.
The @i{operator} is a @i{symbol} that can be the @i{name} of a 
@i{function}, @i{macro}, or @i{special operator}.  
The @i{operator} can be supplied by a keyword option;
it defaults to @i{name}.

Keyword options for the short form are the following:

@table @asis

@item @t{*}  
The @t{:documentation} option is used to document the method-combination type;
see description of long form below.

@item @t{*}  
The @t{:identity-with-one-argument} option enables an optimization
when its value is @i{true} (the default is @i{false}).  If there is
exactly one applicable method and it is a primary method, that method
serves as the effective method and @i{operator} is not called.
This optimization avoids the need to create a new effective method and
avoids the overhead of a @i{function} call.  This option is designed to be
used with operators such as @b{progn}, @b{and}, @b{+}, and
@b{max}.

@item @t{*}  
The @t{:operator} option specifies the @i{name} of the operator.  The
@i{operator} argument is a @i{symbol} that can be the 
@i{name} of a @i{function},
@i{macro}, or 
@i{special form}.  

@end table

These types of method combination require exactly one @i{qualifier} per
method.  An error is signaled if there are applicable methods with no
@i{qualifiers} or with @i{qualifiers} that are not supported by 
the method combination type. 

A method combination procedure defined in this way recognizes two
roles for methods.  A method whose one @i{qualifier} is the symbol naming
this type of method combination is defined to be a primary method.  At
least one primary method must be applicable or an error is signaled.
A method with @t{:around} as its one @i{qualifier} is an auxiliary
method that behaves the same as an @i{around method} in standard
method combination.  The @i{function} @b{call-next-method} can only be
used in @i{around methods}; it cannot be used in primary methods
defined by the short form of the @b{define-method-combination} macro.

A method combination procedure defined in this way accepts an optional
argument named @i{order}, which defaults to 
@t{:most-specific-first}.  A value of @t{:most-specific-last} reverses
the order of the primary methods without affecting the order of the
auxiliary methods.

The short form automatically includes error checking and support for
@i{around methods}.

For a discussion of built-in method combination types, 
see @ref{Built-in Method Combination Types}.

@item @b{Long Form}  
The long form syntax of @b{define-method-combination} is recognized 
when the second @i{subform} is a list.  

The @i{lambda-list} 
receives any arguments provided after the @i{name} of the method
combination type in the @t{:method-combination} option to
@b{defgeneric}.

A list of method group specifiers follows.  Each specifier selects a subset
of the applicable methods to play a particular role, either by matching
their @i{qualifiers} against some patterns or by testing their @i{qualifiers} with
a @i{predicate}.   
These method group specifiers define all method @i{qualifiers}
that can be used with this type of method combination.

The @i{car} of each @i{method-group-specifier} is a @i{symbol}
which @i{names} a @i{variable}.
During the execution of
the @i{forms} in the body of @b{define-method-combination}, this
@i{variable} is bound to a list of the @i{methods} in the method group.  The
@i{methods} in this list occur in the order specified by the 
@t{:order} option.

If @i{qualifier-pattern} is a @i{symbol} it must be @b{*}.  
A method matches
a @i{qualifier-pattern} if the method's 
list of @i{qualifiers} is @b{equal}
to the @i{qualifier-pattern} (except that the symbol @b{*} in a 
@i{qualifier-pattern} matches anything).  Thus 
a @i{qualifier-pattern} can be one of the
following:
 the @i{empty list}, which matches @i{unqualified methods};
 the symbol @b{*}, which matches all methods;
 a true list, which matches methods with the same number of @i{qualifiers} 
   as the length of the list when each @i{qualifier} matches 
   the corresponding list element; or
 a dotted list that ends in the symbol @b{*} 
   (the @b{*} matches any number of additional @i{qualifiers}).

Each applicable method is tested against the @i{qualifier-patterns} and
@i{predicates} in left-to-right order.  
As soon as a @i{qualifier-pattern} matches
or a @i{predicate} returns true, the method becomes a member of the
corresponding method group and no further tests are made.  Thus if a method
could be a member of more than one method group, it joins only the first
such group.  If a method group has more than one 
@i{qualifier-pattern}, a
method need only satisfy one of the @i{qualifier-patterns} to be a member of
the group.

The @i{name} of a @i{predicate} function can appear instead of 
@i{qualifier-patterns} in a method group specifier.  
The @i{predicate} is called for
each method that has not been assigned to an earlier method group; it
is called with one argument, the method's @i{qualifier} @i{list}.
The @i{predicate} should return true if the method is to be a member of the
method group.  A @i{predicate} can be distinguished from a 
@i{qualifier-pattern}
because it is a @i{symbol} other than @b{nil} or @b{*}.

If there is an applicable method that does not fall into any method group,
the @i{function} @b{invalid-method-error} is called.

Method group specifiers can have keyword options following the
@i{qualifier} patterns or predicate.  Keyword options can be distinguished from
additional @i{qualifier} patterns because they are neither lists nor the symbol
@b{*}.  The keyword options are as follows:

@table @asis

@item @t{*}  
The @t{:description} option is used to provide a description of the
role of methods in the method group.  Programming environment tools
use
 @t{(apply #'format stream @i{format-control} (method-qualifiers @i{method}))}
to print this description, which
is expected to be concise.  This keyword
option allows the description of a method @i{qualifier} to be defined in
the same module that defines the meaning of the 
method @i{qualifier}.  In most cases, @i{format-control} will not contain any
@b{format} directives, but they are available for generality.  
If @t{:description} is not supplied, a default description is generated
based on the variable name and the @i{qualifier} patterns and on whether
this method group includes the @i{unqualified methods}.  

@item @t{*}  
The @t{:order} option specifies the order of methods.  The @i{order}
argument is a @i{form} that evaluates to 
@t{:most-specific-first} or @t{:most-specific-last}.  If it evaluates
to any other value, an error is signaled.  
If @t{:order} is not supplied, it defaults to 
@t{:most-specific-first}.

@item @t{*}  
The @t{:required} option specifies whether at least one method in
this method group is required.
If its value is @i{true} and the method group is empty 
(that is, no applicable methods match the @i{qualifier} patterns
or satisfy the predicate), 
an error is signaled.  
If @t{:required} is not supplied,
it defaults to @b{nil}.  

@end table

The use of method group specifiers provides a convenient syntax to
select methods, to divide them among the possible roles, and to perform the
necessary error checking.  It is possible to perform further filtering
of methods in the body @i{forms} by using normal list-processing operations
and the functions @b{method-qualifiers} and 
@b{invalid-method-error}.  It is permissible to use @b{setq} on the
variables named in the method group specifiers and to bind additional
variables.  It is also possible to bypass the method group specifier
mechanism and do everything in the body @i{forms}.  This is accomplished
by writing a single method group with @b{*} as its only 
@i{qualifier-pattern}; 
the variable is then bound to a @i{list} of all of the
@i{applicable methods}, in most-specific-first order.

The body @i{forms} compute and return the @i{form} that specifies
how the methods are combined, that is, the effective method.
The effective method is evaluated in
the @i{null lexical environment} augmented with a local macro definition
for @b{call-method} and with bindings named by
symbols not @i{accessible} from the @t{COMMON-LISP-USER} @i{package}.
Given a method object in one of the 
@i{lists} produced by the method group
specifiers and a @i{list} of next methods,
@b{call-method}
will invoke the method such that @b{call-next-method} has available
the next methods.

When an effective method has no effect other than to call a single
method, some implementations employ an optimization that uses the
single method directly as the effective method, thus avoiding the need
to create a new effective method.  This optimization is active when
the effective method form consists entirely of an invocation of
the @b{call-method} macro whose first @i{subform} is a method object and
whose second @i{subform} is @b{nil} or unsupplied.  Each
@b{define-method-combination} body is responsible for stripping off
redundant invocations of @b{progn}, @b{and},
@b{multiple-value-prog1}, and the like, if this optimization is desired.

The list @t{(:arguments . @i{lambda-list})} can appear before
any declarations or @i{documentation string}.  This form is useful when
the method combination type performs some specific behavior as part of
the combined method and that behavior needs access to the arguments to
the @i{generic function}.  Each parameter variable defined by 
@i{lambda-list} is bound to a @i{form} that can be inserted into the
effective method.  When this @i{form} is evaluated during execution of the
effective method, its value is the corresponding argument to the
@i{generic function}; the consequences of using such a @i{form} as
the @i{place} in a @b{setf} @i{form} are undefined.

Argument correspondence is computed by dividing the @t{:arguments} @i{lambda-list}
and the @i{generic function} @i{lambda-list} into three sections:
     the @i{required parameters},
     the @i{optional parameters},
 and the @i{keyword} and @i{rest parameters}.
The @i{arguments} supplied to the @i{generic function} for a particular @i{call}
are also divided into three sections;
     the required @i{arguments} section contains as many @i{arguments}
      as the @i{generic function} has @i{required parameters},
     the optional @i{arguments} section contains as many arguments
      as the @i{generic function} has @i{optional parameters},
     and the keyword/rest @i{arguments} section contains the remaining arguments.
Each @i{parameter} in the required and optional sections of the 
@t{:arguments} @i{lambda-list} accesses the argument at the same position
in the corresponding section of the @i{arguments}.
If the section of the @t{:arguments} @i{lambda-list} is shorter,
 extra @i{arguments} are ignored. 
If the section of the @t{:arguments} @i{lambda-list} is longer,
 excess @i{required parameters} are bound to forms that evaluate to @b{nil} 
 and excess @i{optional parameters} are @i{bound} to their initforms.
The @i{keyword parameters} and @i{rest parameters} in the @t{:arguments}
@i{lambda-list} access the keyword/rest section of the @i{arguments}.
If the @t{:arguments} @i{lambda-list} contains @b{&key}, it behaves as
if it also contained @b{&allow-other-keys}.

In addition, @b{&whole} @i{var} can be placed first in the @t{:arguments}
@i{lambda-list}.  It causes @i{var} to be @i{bound} to a @i{form}
that @i{evaluates} to a @i{list} of all of the @i{arguments} supplied
to the @i{generic function}.  This is different from @b{&rest} because it
accesses all of the arguments, not just the keyword/rest @i{arguments}.

Erroneous conditions detected by the body should be reported with
@b{method-combination-error} or @b{invalid-method-error}; these
@i{functions}
add any necessary contextual information to the error message and will
signal the appropriate error.

The body @i{forms} are evaluated inside of the @i{bindings} created by
the
@i{lambda list} and method group specifiers. 

[Reviewer Note by Barmar: Are they inside or outside the :ARGUMENTS bindings?]
Declarations at the head of
the body are positioned directly inside of @i{bindings} created by the
@i{lambda list} and outside of the @i{bindings} of the method group variables. 
Thus method group variables cannot be declared in this way.  @b{locally} may be used
around the body, however.

Within the body @i{forms}, @i{generic-function-symbol}
is bound to the @i{generic function} @i{object}.

@i{Documentation} is attached as a @i{documentation string} 
    to @i{name} (as kind @b{method-combination})
and to the @i{method combination} @i{object}.

Note that two methods with identical specializers, but with different
@i{qualifiers}, are not ordered by the algorithm described in Step 2 of
the method selection and combination process described in 
@ref{Method Selection and Combination}.  Normally the two methods play
different roles in the effective method because they have different
@i{qualifiers}, and no matter how they are ordered in the result of Step
2, the effective method is the same.  If the two methods play the same
role and their order matters, 

[Reviewer Note by Barmar: How does the system know when the order matters?]
an error is signaled.  This happens as
part of the @i{qualifier} pattern matching in
@b{define-method-combination}.

@end table

If a @b{define-method-combination} @i{form} appears as a
@i{top level form}, the @i{compiler} must make the
@i{method combination} @i{name} be recognized as a valid
@i{method combination} @i{name} in subsequent @b{defgeneric}
@i{forms}.  However, the @i{method combination} is executed
no earlier than when the @b{define-method-combination} @i{form}
is executed, and possibly as late as the time that @i{generic functions}
that use the @i{method combination} are executed.

@subsubheading  Examples::

Most examples of the long form of @b{define-method-combination} also
illustrate the use of the related @i{functions} that are provided as part
of the declarative method combination facility.

@example
;;; Examples of the short form of define-method-combination

 (define-method-combination and :identity-with-one-argument t) 

 (defmethod func and ((x class1) y) ...)

;;; The equivalent of this example in the long form is:

 (define-method-combination and 
         (&optional (order :most-specific-first))
         ((around (:around))
          (primary (and) :order order :required t))
   (let ((form (if (rest primary)
                   `(and ,@@(mapcar #'(lambda (method)
                                       `(call-method ,method))
                                   primary))
                   `(call-method ,(first primary)))))
     (if around
         `(call-method ,(first around)
                       (,@@(rest around)
                        (make-method ,form)))
         form)))

;;; Examples of the long form of define-method-combination

;The default method-combination technique
 (define-method-combination standard ()
         ((around (:around))
          (before (:before))
          (primary () :required t)
          (after (:after)))
   (flet ((call-methods (methods)
            (mapcar #'(lambda (method)
                        `(call-method ,method))
                    methods)))
     (let ((form (if (or before after (rest primary))
                     `(multiple-value-prog1
                        (progn ,@@(call-methods before)
                               (call-method ,(first primary)
                                            ,(rest primary)))
                        ,@@(call-methods (reverse after)))
                     `(call-method ,(first primary)))))
       (if around
           `(call-method ,(first around)
                         (,@@(rest around)
                          (make-method ,form)))
           form))))

;A simple way to try several methods until one returns non-nil
 (define-method-combination or ()
         ((methods (or)))
   `(or ,@@(mapcar #'(lambda (method)
                      `(call-method ,method))
                  methods)))

;A more complete version of the preceding
 (define-method-combination or 
         (&optional (order ':most-specific-first))
         ((around (:around))
          (primary (or)))
   ;; Process the order argument
   (case order
     (:most-specific-first)
     (:most-specific-last (setq primary (reverse primary)))
     (otherwise (method-combination-error "~S is an invalid order.~@@
     :most-specific-first and :most-specific-last are the possible values."
                                          order)))
   ;; Must have a primary method
   (unless primary
     (method-combination-error "A primary method is required."))
   ;; Construct the form that calls the primary methods
   (let ((form (if (rest primary)
                   `(or ,@@(mapcar #'(lambda (method)
                                      `(call-method ,method))
                                  primary))
                   `(call-method ,(first primary)))))
     ;; Wrap the around methods around that form
     (if around
         `(call-method ,(first around)
                       (,@@(rest around)
                        (make-method ,form)))
         form)))

;The same thing, using the :order and :required keyword options
 (define-method-combination or 
         (&optional (order ':most-specific-first))
         ((around (:around))
          (primary (or) :order order :required t))
   (let ((form (if (rest primary)
                   `(or ,@@(mapcar #'(lambda (method)
                                      `(call-method ,method))
                                  primary))
                   `(call-method ,(first primary)))))
     (if around
         `(call-method ,(first around)
                       (,@@(rest around)
                        (make-method ,form)))
         form)))

;This short-form call is behaviorally identical to the preceding
 (define-method-combination or :identity-with-one-argument t)

;Order methods by positive integer qualifiers
;:around methods are disallowed to keep the example small
 (define-method-combination example-method-combination ()
         ((methods positive-integer-qualifier-p))
   `(progn ,@@(mapcar #'(lambda (method)
                         `(call-method ,method))
                     (stable-sort methods #'<
                       :key #'(lambda (method)
                                (first (method-qualifiers method)))))))

 (defun positive-integer-qualifier-p (method-qualifiers)
   (and (= (length method-qualifiers) 1)
        (typep (first method-qualifiers) '(integer 0 *))))

;;; Example of the use of :arguments
 (define-method-combination progn-with-lock ()
         ((methods ()))
   (:arguments object)
   `(unwind-protect
        (progn (lock (object-lock ,object))
               ,@@(mapcar #'(lambda (method)
                             `(call-method ,method))
                         methods))
      (unlock (object-lock ,object))))

@end example

@subsubheading  Side Effects::

The @i{compiler} is not required to perform any compile-time side-effects.

@subsubheading  Exceptional Situations::

Method combination types defined with the short form require exactly
one @i{qualifier} per method.  
An error of @i{type} @b{error} is signaled if there are
applicable methods with no @i{qualifiers} or with @i{qualifiers} that are not
supported by the method combination type.
At least one primary method must be applicable or 
an error of @i{type} @b{error} is signaled.

If an applicable method does not fall into any method group, the
system signals an error of @i{type} @b{error}
indicating that the method is invalid for the kind of
method combination in use.

If the value of the @t{:required} option is @i{true}
and the method group is empty (that is, no applicable
methods match the @i{qualifier} patterns or satisfy the predicate), 
an error of @i{type} @b{error} is signaled.

If the @t{:order} option evaluates to a value other than 
@t{:most-specific-first} or @t{:most-specific-last}, 
an error of @i{type} @b{error} is signaled.

@subsubheading  See Also:: 

@ref{call-method}
,
@ref{call-next-method}
,
@ref{documentation}
,
@ref{method-qualifiers}
,
@ref{method-combination-error}
,
@ref{invalid-method-error}
,
@ref{defgeneric}
,
@ref{Method Selection and Combination},
@ref{Built-in Method Combination Types},
@ref{Syntactic Interaction of Documentation Strings and Declarations}

@subsubheading  Notes::

The @t{:method-combination} option of @b{defgeneric} is used to
specify that a @i{generic function} should use a particular method
combination type.  The first argument to the @t{:method-combination}
option is the @i{name} of a method combination type and the remaining
arguments are options for that type.

@node find-method, add-method, define-method-combination, Objects Dictionary
@subsection find-method                                     [Standard Generic Function]

@subsubheading  Syntax::

@code{find-method}  @i{generic-function method-qualifiers specializers @r{&optional} errorp}@*
   @result{}  @i{method}

@subsubheading  Method Signatures::

@code{find-method}  @i{@r{(}@i{generic-function} @b{standard-generic-function}@r{)}
		method-qualifiers specializers @r{&optional} errorp}

@subsubheading  Arguments and Values::

@i{generic-function}---a @i{generic function}.

@i{method-qualifiers}---a @i{list}.

@i{specializers}---a @i{list}.

@i{errorp}---a @i{generalized boolean}.
 The default is @i{true}.

@i{method}---a @i{method} @i{object}, or @b{nil}.

@subsubheading  Description::

The @i{generic function} @b{find-method} takes a @i{generic function} 
and returns the @i{method} @i{object} that agrees on @i{qualifiers} 
and @i{parameter specializers} with the @i{method-qualifiers} and 
@i{specializers} arguments of @b{find-method}.  
@i{Method-qualifiers}  contains the
method @i{qualifiers} for the @i{method}. 
The order of the method @i{qualifiers}
is significant.                                
For a definition of agreement in this context,
see @ref{Agreement on Parameter Specializers and Qualifiers}.

The @i{specializers} argument contains the parameter
specializers for the @i{method}. It must correspond in length to
the number of required arguments of the @i{generic function}, or
an error is signaled.  This means that to obtain the
default @i{method} on a given @i{generic-function},
a @i{list} whose elements are the @i{class} @b{t} must be given.

If there is no such @i{method} and @i{errorp} is @i{true},
@b{find-method} signals an error.
If there is no such @i{method} and @i{errorp} is @i{false},
@b{find-method} returns @b{nil}.

@subsubheading  Examples::

@example
 (defmethod some-operation ((a integer) (b float)) (list a b))
@result{}  #<STANDARD-METHOD SOME-OPERATION (INTEGER FLOAT) 26723357>
 (find-method #'some-operation '() (mapcar #'find-class '(integer float)))
@result{}  #<STANDARD-METHOD SOME-OPERATION (INTEGER FLOAT) 26723357>
 (find-method #'some-operation '() (mapcar #'find-class '(integer integer)))
@t{ |> } Error: No matching method
 (find-method #'some-operation '() (mapcar #'find-class '(integer integer)) nil)
@result{}  NIL
@end example

@subsubheading  Affected By::

@b{add-method},
@b{defclass},
@b{defgeneric},
@b{defmethod}

@subsubheading  Exceptional Situations::

If the @i{specializers} argument does not correspond in length to
the number of required arguments of the @i{generic-function}, an
an error of @i{type} @b{error} is signaled.  

If there is no such @i{method} and @i{errorp} is @i{true}, 
@b{find-method} signals an error of @i{type} @b{error}.

@subsubheading  See Also::

@ref{Agreement on Parameter Specializers and Qualifiers}

@node add-method, initialize-instance, find-method, Objects Dictionary
@subsection add-method                                      [Standard Generic Function]

@subsubheading  Syntax::

@code{add-method}  @i{generic-function method} @result{}  @i{generic-function}

@subsubheading  Method Signatures::

@code{add-method}  @i{@r{(}@i{generic-function} @b{standard-generic-function}@r{)}
			   @r{(}@i{method} @b{method}@r{)}}

@subsubheading  Arguments and Values::

@i{generic-function}---a @i{generic function} @i{object}.

@i{method}---a @i{method} @i{object}.

@subsubheading  Description::

The generic function @b{add-method} adds a @i{method}
to a @i{generic function}.

If @i{method} agrees with an existing @i{method} of @i{generic-function}
on @i{parameter specializers} and @i{qualifiers}, 
the existing @i{method} is replaced.

@subsubheading  Exceptional Situations::

The @i{lambda list} of the method function of @i{method} must be
congruent with the @i{lambda list} of @i{generic-function}, 
or an error of @i{type} @b{error} is signaled.

If @i{method} is a @i{method} @i{object} of
another @i{generic function}, an error of @i{type} @b{error} is signaled.

@subsubheading  See Also::

@ref{defmethod}
,
@ref{defgeneric}
,
@ref{find-method}
,
@ref{remove-method}
,
@ref{Agreement on Parameter Specializers and Qualifiers}

@node initialize-instance, class-name, add-method, Objects Dictionary
@subsection initialize-instance                             [Standard Generic Function]

@subsubheading  Syntax::

@code{initialize-instance}  @i{instance @r{&rest} initargs @r{&key} @r{&allow-other-keys}} @result{}  @i{instance}

@subsubheading  Method Signatures::

@code{initialize-instance}  @i{@r{(}@i{instance} @b{standard-object}@r{)} @r{&rest} initargs}

@subsubheading  Arguments and Values::

@i{instance}---an @i{object}.

@i{initargs}---a @i{defaulted initialization argument list}.

@subsubheading  Description::

Called by @b{make-instance} to initialize a newly created @i{instance}.
The generic function is called with the new @i{instance} 
and the @i{defaulted initialization argument list}.

The system-supplied primary @i{method} on @b{initialize-instance}
initializes the @i{slots} of the @i{instance} with values according 
to the @i{initargs} and the @t{:initform} forms of the @i{slots}.
It does this by calling the generic function @b{shared-initialize}
with the following arguments: the @i{instance}, @b{t} (this indicates
that all @i{slots} for which no initialization arguments are provided
should be initialized according to their @t{:initform} forms), and
the @i{initargs}.

Programmers can define @i{methods} for @b{initialize-instance} to
specify actions to be taken when an instance is initialized.  If only
@i{after methods} are defined, they will be run after the
system-supplied primary @i{method} for initialization and therefore will
not interfere with the default behavior of @b{initialize-instance}.

@subsubheading  See Also::

@ref{Shared-Initialize}
,
@ref{make-instance}
,
@ref{slot-boundp}
,
@ref{slot-makunbound}
,
@ref{Object Creation and Initialization},
@ref{Rules for Initialization Arguments},
@ref{Declaring the Validity of Initialization Arguments}

@node class-name, setf class-name, initialize-instance, Objects Dictionary
@subsection class-name                                      [Standard Generic Function]

@subsubheading  Syntax::

@code{class-name}  @i{class} @result{}  @i{name}

@subsubheading  Method Signatures::

@code{class-name}  @i{@r{(}@i{class} @b{class}@r{)}}

@subsubheading  Arguments and Values::

@i{class}---a @i{class} @i{object}.

@i{name}---a @i{symbol}.

@subsubheading  Description::

Returns the @i{name} of the given @i{class}.

@subsubheading  See Also::

@ref{find-class}
,
@ref{Classes}

@subsubheading  Notes::

If S is a @i{symbol} such that S =@t{(class-name C)}
and C =@t{(find-class S)}, then S is the proper name of C.
For further discussion, see @ref{Classes}.

The name of an anonymous @i{class} is @b{nil}.

@node setf class-name, class-of, class-name, Objects Dictionary
@subsection setf class-name                               [Standard Generic Function]

@subsubheading  Syntax::

@code{setf class-name}  @i{new-value class} @result{}  @i{new-value}

@subsubheading  Method Signatures::

@code{setf class-name}  @i{new-value @r{(}@i{class} @b{class}@r{)}}

@subsubheading  Arguments and Values::

@i{new-value}---a @i{symbol}.

@i{class}---a @i{class}.

@subsubheading  Description::

The generic function @t{setf class-name} sets the name of 
a @i{class} object.

@subsubheading  See Also::

@ref{find-class}
,
@i{proper name},
@ref{Classes}

@node class-of, unbound-slot, setf class-name, Objects Dictionary
@subsection class-of                                                         [Function]

@code{class-of}  @i{object} @result{}  @i{class}

@subsubheading  Arguments and Values::

@i{object}---an @i{object}.

@i{class}---a @i{class} @i{object}.

@subsubheading  Description::

Returns the @i{class} of which the @i{object} is 
a @i{direct instance}.

@subsubheading  Examples::

@example
 (class-of 'fred) @result{}  #<BUILT-IN-CLASS SYMBOL 610327300>
 (class-of 2/3) @result{}  #<BUILT-IN-CLASS RATIO 610326642>

 (defclass book () ()) @result{}  #<STANDARD-CLASS BOOK 33424745>
 (class-of (make-instance 'book)) @result{}  #<STANDARD-CLASS BOOK 33424745>

 (defclass novel (book) ()) @result{}  #<STANDARD-CLASS NOVEL 33424764>
 (class-of (make-instance 'novel)) @result{}  #<STANDARD-CLASS NOVEL 33424764>

 (defstruct kons kar kdr) @result{}  KONS
 (class-of (make-kons :kar 3 :kdr 4)) @result{}  #<STRUCTURE-CLASS KONS 250020317>
@end example

@subsubheading  See Also::

@ref{make-instance}
,
@ref{type-of}

@node unbound-slot, unbound-slot-instance, class-of, Objects Dictionary
@subsection unbound-slot                                               [Condition Type]

@subsubheading  Class Precedence List::
@b{unbound-slot},
@b{cell-error},
@b{error},
@b{serious-condition},
@b{condition},
@b{t}

@subsubheading  Description::

The @i{object} having the unbound slot is initialized by 
the @t{:instance} initialization argument to @b{make-condition},
and is @i{accessed} by the @i{function} @b{unbound-slot-instance}.

The name of the cell (see @b{cell-error}) is the name of the slot.

@subsubheading  See Also::

@ref{cell-error-name}
,
@b{unbound-slot-object},
@ref{Condition System Concepts}

@node unbound-slot-instance,  , unbound-slot, Objects Dictionary
@subsection unbound-slot-instance                                            [Function]

@code{unbound-slot-instance}  @i{condition} @result{}  @i{instance}

@subsubheading  Arguments and Values::

@i{condition}---a @i{condition} of @i{type} @b{unbound-slot}.

@i{instance}---an @i{object}.

@subsubheading  Description::

Returns the instance which had the unbound slot in the @i{situation}
represented by the @i{condition}.          

@subsubheading  See Also::

@ref{cell-error-name}
,
@b{unbound-slot},
@ref{Condition System Concepts}

@c end of including dict-objects

@c %**end of chapter

