29.10.1. Instance Structure Protocol

The instance structure protocol is responsible for implementing
the behavior of the slot access functions like SLOT-VALUE and
(SETFSLOT-VALUE).

For each CLOS slot access function other than SLOT-EXISTS-P,
there is a corresponding generic function which actually provides the
behavior of the function. When called, the slot access function finds
the pertinent effectiveslot definition metaobject, calls the corresponding generic function and
returns its result. The arguments passed on to the generic function
include one additional value, the class of the object argument,
which always immediately precedes the object argument.

At the lowest level, the instance structure protocol provides only
limited mechanisms for portable programs to control the implementation
of instances and to directly access the storage associated with
instances without going through the indirection of slot access. This is
done to allow portable programs to perform certain commonly requested
slot access optimizations.

In particular, portable programs can control the implementation
of, and obtain direct access to, slots with allocation :INSTANCE and
type T. These are called directly accessible slots
.

The relevant specified around-method on CLOS:COMPUTE-SLOTS determines
the implementation of instances by deciding how each slot in the
instance will be stored. For each directly accessible slot, this method
allocates a location
and associates it with the effectiveslot definition metaobject.
The location can be accessed by calling the CLOS:SLOT-DEFINITION-LOCATION
generic function. Locations are non-negative integers. For a given
class, the locations increase consecutively, in the order that the
directly accessible slots appear in the list of effective slots. (Note
that here, the next paragraph, and the specification of this
around-method are the only places where the value returned by
CLOS:COMPUTE-SLOTS is described as a list rather than a set.)

Portable programs are permitted to affect and rely on the
allocation of locations only in the following limited way: By first
defining a portable primary method on CLOS:COMPUTE-SLOTS which orders the
returned value in a predictable way, and then relying on the defined
behavior of the specified around-method to assign locations to all
directly accessible slots. Portable programs may compile-in calls to
low-level accessors which take advantage of the resulting predictable
allocation of slot locations.

This example shows the use of this mechanism to implement a new
class metaobject class, ordered-class and class
option :SLOT-ORDER. This option provides control
over the allocation of slot locations. In this simple example
implementation, the :SLOT-ORDER option is not
inherited by subclasses; it controls only instances of the class
itself.

Following is the source code the user of this extension would write.
Note that because the code above does not implement inheritance of
the :SLOT-ORDER option, the function
distance must not be called on instances of
subclasses of point; it can only be called on
instances of point itself.

Implementation dependent: only in CLISP

You cannot assume that the slot-location
values start at 0. In class point, for
example, x and y will be at slot locations 1 and 2, not 0 and
1.

In more realistic uses of this mechanism, the calls to the low-level
instance structure accessors would not actually appear textually in the
source program, but rather would be generated by a meta-level analysis
program run during the process of compiling the source program.

Like standard instances, funcallable instances have slots with the
normal behavior. They differ from standard instances in that they can
be used as functions as well; that is, they can be passed to FUNCALL
and APPLY, and they can be stored as the definition of a function
name. Associated with each funcallable instance is the function which
it runs when it is called. This function can be changed with
CLOS:SET-FUNCALLABLE-INSTANCE-FUNCTION.

The following simple example shows the use of funcallable
instances to create a simple, DEFSTRUCT-like facility. (Funcallable
instances are useful when a program needs to construct and maintain a
set of functions and information about those functions. They make it
possible to maintain both as the same object rather than two separate
objects linked, for example, by hash tables.)

This function is called to provide direct access to
a slot in an instance. By usurping the normal slot lookup protocol,
this function is intended to provide highly optimized access to the
slots associated with an instance.

This function is called to provide direct access to
a slot in an instance. By usurping the normal slot lookup protocol,
this function is intended to provide highly optimized access to the
slots associated with an instance.

This function is called to set or to change the
function of a funcallable instance. After
CLOS:SET-FUNCALLABLE-INSTANCE-FUNCTION is called, any subsequent calls
to funcallable-instance will run the new
function.

These methods implement
the full behavior of this generic function for slots with allocation
:INSTANCE and :CLASS. If the supplied slot has an allocation
other than :INSTANCE or :CLASS an ERROR is SIGNALed.

Overriding these methods is permitted,
but may require overriding other methods in the standard implementation
of the slot access protocol.

These methods implement
the full behavior of this generic function for slots with allocation
:INSTANCE and :CLASS. If the supplied slot has an allocation
other than :INSTANCE or :CLASS an ERROR is SIGNALed.

Overriding these methods is permitted,
but may require overriding other methods in the standard implementation
of the slot access protocol.

These methods implement
the full behavior of this generic function for slots with allocation
:INSTANCE and :CLASS. If the supplied slot has an allocation
other than :INSTANCE or :CLASS an ERROR is SIGNALed.

Overriding these methods is permitted,
but may require overriding other methods in the standard implementation
of the slot access protocol.

These methods implement
the full behavior of this generic function for slots with allocation
:INSTANCE and :CLASS. If the supplied slot has an allocation
other than :INSTANCE or :CLASS an ERROR is SIGNALed.

Overriding these methods is permitted,
but may require overriding other methods in the standard implementation
of the slot access protocol.