forked from filonenko-mikhail/cltl2-doc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
clos.tex
10651 lines (8695 loc) · 579 KB
/
clos.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%Part{CLOS, Root = "CLM.MSS"}
%%% Chapter of Common Lisp Manual. Copyright 1984, 1987, 1988, 1989 Guy L. Steele Jr.
\clearpage\def\pagestatus{FINAL PROOF}
%\begingroup
\ifx \rulang\Undef
\def\CLOS{Common Lisp Object System}
\def\OS{Object System}
\else %RUSSIAN
\def\CLOS{Объектная система Common Lisp'а}
\def\OS{Объектная система}
\fi
\def\sub{_}
\mathchardef\mhyphen="2D
\ifx \rulang\Undef
\chapter{Common Lisp Object System}
\label{CLOS}
Authors: Daniel G.~Bobrow, Linda G.~DeMichiel,
Richard P.~Gabriel, Sonya E.~Keene, Gregor Kiczales,
and David A.~Moon
% \begin{new}
% X3J13 voted in June 1988
% \issue{CLOS}
% to adopt the first two chapters (of three) of the
% Common Lisp Object System specification
% as a part of the forthcoming draft Common Lisp standard.
% \end{new}
This chapter presents the bulk of the first two chapters of the
Common Lisp Object System specification; it is substantially
identical to these two specification chapters as previously published elsewhere
\cite{SIGPLAN-CLOS,LASC-CLOS-PART-1,LASC-CLOS-PART-2}.
I have edited the material only very lightly
to conform to the overall style of this book and to save a substantial
number of pages by using a typographically condensed presentation.
I have inserted a small
number of bracketed remarks, identified by the initials GLS.
The chapter divisions of the original specification have become
section divisions in this chapter; references to the three chapters
of the original specification now refer to the three ``parts'' of the
specification.
(See the Acknowledgments to this second edition for
acknowledgments to others who contributed to the Common Lisp Object System specification.)
This is not the last word on CLOS;
X3J13 may well refine this material further.
Keene has written a good tutorial introduction to CLOS~\cite{KEENE}.
\noindent\hbox to \textwidth{\hss---Guy L. Steele Jr.}
\vskip 8pt plus 3pt minus 2pt
\section{Programmer Interface Concepts}
The \CLOS\ (CLOS) is an object-oriented extension to Common Lisp. It is based on
generic functions, multiple inheritance, declarative method
combination, and a meta-object protocol.
The first two parts of this specification describe
the standard Programmer Interface for the \CLOS. The first part,
Programmer Interface Concepts,
contains a description of the concepts of the \CLOS, and the second part,
Functions in the Programmer Interface,
contains a description of the functions and macros in the \CLOS\
Programmer Interface. The third part, The \CLOS\ Meta-Object
Protocol, explains how the \CLOS\ can be customized.
The fundamental objects of the \CLOS\ are classes, instances,
generic functions, and methods.
A \emph{class} object determines the structure and behavior of a set
of other objects, which are called its \emph{instances}.
Every Common Lisp object is an \emph{instance} of a class. The class of an
object determines the set of operations that can be performed on the object.
A \emph{generic function} is a function whose behavior depends on the
classes or identities of the arguments supplied to it. A generic
function object contains a set of methods, a lambda-list, a
method combination type, and other information. The \emph{methods} define
the class-specific behavior and operations of the generic function; a
method is said to \emph{specialize} a generic function. When invoked,
a generic function executes a subset of its methods based on the
classes of its arguments.
A generic function can be used in
the same ways as an ordinary function in Common Lisp; in
particular, a generic function can be used as an argument to
\cdf{funcall} and \cdf{apply} and can be given a global name.%or a local name.
A \emph{method} is an object that contains a method function, a sequence of
\emph{parameter specializers} that specify when the given method is
applicable, and a sequence of \emph{qualifiers} that is used by the
\emph{method combination} facility to distinguish among methods. Each
required formal parameter of each method has an associated parameter
specializer, and the method will be invoked only on arguments that
satisfy its parameter specializers.
The method combination facility controls the selection of methods, the
order in which they are run, and the values that are returned by the
generic function. The \CLOS\ offers a default method combination type
and provides a facility for declaring new types of method combination.
\subsection{Error Terminology}
\label{Error-Terminology-SECTION}
%The terminology used in this chapter to describe erroneous
%situations differs from the terminology used in the first edition.
%The new terminology involves \emph{situations};
A situation is the evaluation of an expression in some
specific context. For example, a situation might be the invocation of
a function on arguments that fail to satisfy some specified
constraints.
In the specification of the \CLOS, the behavior of programs in all situations
is described, and the options available to the implementor are defined. No
implementation is allowed to extend the syntax or semantics of the \OS\ except
as explicitly defined in the \OS\ specification. In particular, no
implementation is allowed to extend the syntax of the \OS\ in such a way that
ambiguity between the specified syntax of the \OS\ and those extensions is
possible.
\begin{flushdesc}
\item[``When situation \emph{S} occurs, an error is signaled.'']
This terminology has the following meaning:
\begin{itemize}
\item If this situation occurs, an error will be signaled in
the interpreter and in code compiled under all compiler safety
optimization levels.
\item Valid programs may rely on the fact that an error will be
signaled in the interpreter and in code compiled under all compiler
safety optimization levels.
\item Every implementation is required to detect such an error
in the interpreter and in code compiled under all compiler safety
optimization levels.
\end{itemize}
\item[``When situation \emph{S} occurs, an error should be signaled.'']
This terminology has the following meaning:
\begin{itemize}
\item If this situation occurs, an error will be signaled at
least in the interpreter and in code compiled under the safest
compiler safety optimization level.
\item Valid programs may not rely on the fact that an error will be
signaled.
\item Every implementation is required to detect such an error
at least in the interpreter and in code compiled under the safest
compiler safety optimization level.
\item When an error is not signaled, the results are undefined (see
below).
\end{itemize}
\item[``When situation \emph{S} occurs, the results are undefined.'']
This terminology has the following meaning:
\begin{itemize}
\item If this situation occurs, the results are unpredictable. The
results may range from harmless to fatal.
\item Implementations are allowed to detect this situation and
signal an error, but no implementation is required to detect the
situation.
\item No valid program may depend on the effects of this
situation, and all valid programs are required to treat the effects
of this situation as unpredictable.
\end{itemize}
\item[``When situation \emph{S} occurs, the results are unspecified.'']
This terminology has the following meaning:
\begin{itemize}
\item The effects of this situation are not specified in
the \OS, but the effects are harmless.
\item Implementations are allowed to specify the effects of
this situation.
\item No portable program can depend on the effects of this
situation, and all portable programs are required to treat the situation
as unpredictable but harmless.
\end{itemize}
\item[``The \CLOS\ may be extended to cover situation \emph{S}.'']
The meaning of this terminology is that an implementation is free to treat
situation \emph{S} in one of three ways:
\begin{itemize}
\item When situation \emph{S} occurs, an error is signaled at least
in the interpreter and in code compiled under the safest compiler
safety optimization level.
\item When situation \emph{S} occurs, the results are undefined.
\item When situation \emph{S} occurs, the results are defined and
specified.
\end{itemize}
\noindent
In addition, this terminology has the following meaning:
\begin{itemize}
\item No portable program can depend on the effects of this
situation, and all portable programs are required to treat the situation
as undefined.
\end{itemize}
\item[``Implementations are free to extend the syntax \emph{S}.'']
This terminology has the following meaning:
\begin{itemize}
\item Implementations are allowed to define unambiguous extensions
to syntax \emph{S}.
\item No portable program can depend on this extension, and
all portable programs are required to treat the syntax
as meaningless.
\end{itemize}
\end{flushdesc}
The \CLOS\ specification may disallow certain extensions while allowing others.
\subsection{Classes}
\label{Classes-SECTION}
A \emph{class} is an object that determines the structure and behavior
of a set of other objects, which are called its \emph{instances}.
A class can inherit structure and behavior from other classes.
A class whose definition refers to other classes for the purpose of
inheriting from them is said to be a \emph{subclass} of each of
those classes. The classes that are designated for purposes of
inheritance are said to be \emph{superclasses}
of the inheriting class.
A class can have a \emph{name}. The function \cdf{class-name} takes a
class object and returns its name. The name of an anonymous class is
\cdf{nil}. A symbol can \emph{name} a class. The function
\cdf{find-class} takes a symbol and returns the class that the symbol
names. A class has a \emph{proper name} if the name is a symbol
and if the name of the class
names that class. That is, a class~\emph{C} has the \emph{proper
name}~\emph{S} if \emph{S}~= \cd{(class-name \emph{C})} and \emph{C}~= \cd{(find-class
\emph{S})}. Notice that it is possible for \cd{(find-class $S_1$)}
= \cd{(find-class $S_2$)} and $S_1\neq S_2$.
If \emph{C}~= \cd{(find-class \emph{S})}, we say that \emph{C} is the \emph{class named}
\emph{S}.
A class $C_{1}$ is a \emph{direct superclass} of a class
$C_{2}$ if $C_{2}$ explicitly designates $C_{1}$ as a
superclass in its definition. In this case, $C_{2}$ is a \emph{direct
subclass} of $C_{1}$. A class $C_{n}$ is a \emph{superclass} of a
class $C_{1}$ if there exists a series of
classes $C_{2},\ldots,C_{n-1}$
such that $C_{i+1}$ is a
direct superclass of $C_{i}$ for $1 \leq \emph{i} < \emph{n}$. In this case,
$C_{1}$ is a \emph{subclass} of $C_{n}$. A class is
considered neither a superclass nor a subclass of itself. That is, if
$C_{1}$ is a superclass of $C_{2}$, then $C_{1} \neq
C_{2}$. The set of classes consisting of some given
class \emph{C} along with all of its superclasses is called ``\emph{C} and its
superclasses.''
Each class has a \emph{class precedence list}, which is a total ordering
on the set of the given class and its superclasses. The total ordering
is expressed as a list ordered from most specific to least specific.
The class precedence list is used in several ways. In general, more
specific classes can \emph{shadow}, or override, features that would
otherwise be inherited from less specific classes. The method selection
and combination process uses the class precedence list to order methods
from most specific to least specific.
When a class is defined, the order in which its direct superclasses
are mentioned in the defining form is important. Each class has a
\emph{local precedence order}, which is a list consisting of the
class followed by its direct superclasses in the order mentioned
in the defining form.
A class precedence list is always consistent with the local precedence
order of each class in the list. The classes in each local precedence
order appear within the class precedence list in the same order. If
the local precedence orders are inconsistent with each other, no class
precedence list can be constructed, and an error is signaled.
The class precedence list and its computation is discussed
in section \ref{Determining-the-Class-Precedence-List-SECTION}.
Classes are organized into a \emph{directed acyclic graph}. There are
two distinguished classes, named \cdf{t} and \cdf{standard-object}.
The class named \cdf{t} has no superclasses. It is a superclass of
every class except itself. The class named \cdf{standard-object} is
an instance of the class \cdf{standard-class} and is a superclass of
every class that is an instance of \cdf{standard-class} except itself.
There is a mapping from the Common Lisp Object System class space into
the Common Lisp type space. Many of the standard Common Lisp types
have a corresponding
class that has the same name as the type. Some Common Lisp types do
not have a corresponding class. The integration of the type and class
systems is discussed in section~\ref{Integrating-Types-and-Classes-SECTION}.
Classes are represented by objects that are themselves
instances of classes. The class of the class of an object is termed
the \emph{metaclass} of that object. When no misinterpretation is
possible, the term \emph{metaclass} will be used to refer to a class
that has instances that are themselves classes. The metaclass
determines the form of inheritance used by the classes that are its
instances and the representation of the instances of those classes.
The \CLOS\ provides a default metaclass, \cdf{standard-class}, that is
appropriate for most programs. The meta-object protocol provides
mechanisms for defining and using new metaclasses.
Except where otherwise specified, all classes mentioned in this
chapter are instances of the class \cdf{standard-class}, all generic
functions are instances of the class \cdf{standard-generic-function},
and all methods are instances of the class \cdf{standard-method}.
\subsubsection{Defining Classes}
The macro \cdf{defclass} is used to define a new named class.
The definition of a class includes the following:
\begin{itemize}
\item The name of the new class. For newly defined classes
this is a proper name.
\item The list of the direct superclasses of the new class.
\item A set of \emph{slot specifiers}. Each slot specifier
includes the name of the slot and zero or more \emph{slot options}. A
slot option pertains only to a single slot. If a class definition
contains two slot specifiers with the same name, an error is signaled.
\item A set of \emph{class options}. Each class option pertains
to the class as a whole.
\end{itemize}
The slot options and class options of the \cdf{defclass} form provide
mechanisms for the following:
\begin{itemize}
\item Supplying a default initial value form for a given slot.
\item Requesting that methods for generic functions
be automatically generated for reading or writing slots.
\item Controlling whether a given slot is shared by instances
of the class or whether each instance of the class has its own slot.
\item Supplying a set of initialization arguments and initialization
argument defaults to be used in instance creation.
%\item Requesting that a constructor function be automatically
%generated for making instances of the new class.
\item Indicating that the metaclass is to be other than the default.
\item Indicating the expected type for the value stored in the slot.
\item Indicating the documentation string for the slot.
\end{itemize}
\subsubsection{Creating Instances of Classes}
The generic function \cdf{make-instance} creates and returns a new
instance of a class. The \OS\ provides several mechanisms for
specifying how a new instance is to be initialized. For example, it
is possible to specify the initial values for slots in newly created
instances either by giving arguments to \cdf{make-instance} or by
providing default initial values.
Further initialization activities
can be performed by methods written for generic functions that are
part of the initialization protocol. The complete initialization
protocol is described in
section~\ref{Object-Creation-and-Initialization-SECTION}.
\subsubsection{Slots}
An object that has \cdf{standard-class} as its metaclass has zero or
more named slots. The slots of an object are determined by the class
of the object. Each slot can hold one value. The name of a slot is a
symbol that is syntactically valid for use as a variable
name.
When a slot does not have a value, the slot is said to be \emph{unbound}. When
an unbound slot is read, the generic function \cdf{slot-unbound} is invoked. The
system-supplied primary method for \cdf{slot-unbound} signals an error.
The default initial value form for a slot is defined by the \cd{:initform} slot
option. When the \cd{:initform} form is used to
supply a value, it is evaluated in the lexical environment in which
the \cdf{defclass} form was evaluated. The \cd{:initform} along with
the lexical environment in which the \cdf{defclass} form was evaluated
is called a \emph{captured} \cd{:initform}.
See section~\ref{Object-Creation-and-Initialization-SECTION}.
A \emph{local slot} is defined to be a slot that is visible to exactly
one instance, namely the one in which the slot is allocated. A \emph{shared
slot} is defined to be a slot that is visible to more than one instance of a
given class and its subclasses.
A class is said to \emph{define} a slot with a given name when
the \cdf{defclass} form for that class contains a slot specifier with
that name. Defining a local slot does not immediately create a slot;
it causes a slot to be created each time an instance of the class is
created. Defining a shared slot immediately creates a slot.
The \cd{:allocation} slot option to \cdf{defclass} controls the kind
of slot that is defined. If the value of the \cd{:allocation} slot
option is \cd{:instance}, a local slot is created. If the value of
\cd{:allocation} is \cd{:class}, a shared slot is created.
A slot is said to be \emph{accessible} in an instance of a class if
the slot is defined by the class of the instance or is inherited from
a superclass of that class. At most one slot of a given name can be
accessible in an instance. A shared slot defined by a class is
accessible in all instances of that class. A detailed explanation of
the inheritance of slots is given in
section~\ref{Inheritance-of-Slots-and-Slot-Options-SECTION}.
\subsubsection{Accessing Slots}
Slots can be accessed in two ways: by use of the primitive function
\cdf{slot-value} and by use of generic functions generated by the
\cdf{defclass} form.
The function \cdf{slot-value} can be used with any slot name
specified in the \cdf{defclass} form to access a specific slot
accessible in an instance of the given class.
The macro \cdf{defclass} provides syntax for generating methods to
read and write slots. If a \emph{reader} is requested, a method is
automatically generated for reading the value of the slot, but no
method for storing a value into it is generated. If a \emph{writer}
is requested, a method is automatically generated for storing a value
into the slot, but no method for reading its value is generated. If
an \emph{accessor} is requested, a method for reading the value of
the slot and a method for storing a value into the slot are
automatically generated. Reader and writer methods are implemented
using \cdf{slot-value}.
When a reader or writer is specified for a slot, the name of the
generic function to which the generated method belongs is directly
specified. If the name specified for the writer option is the symbol
\emph{name}, the name of the generic function for writing the slot
is the symbol \emph{name}, and the generic function takes two
arguments: the new value and the instance, in that order. If the name
specified for the accessor option is the symbol \emph{name}, the
name of the generic function for reading the slot is the symbol \emph{name}, and
the name of the generic function for writing the slot is
the list \cd{(setf \emph{name})}.
A generic function created or modified by supplying reader, writer, or
accessor slot options can be treated exactly as an ordinary generic
function.
Note that \cdf{slot-value} can be used to read or write the value of a
slot whether or not reader or writer methods exist for that slot.
When \cdf{slot-value} is used, no reader or writer methods are
invoked.
The macro \cdf{with-slots} can be used to establish a lexical
environment in which specified slots are lexically available as if they
were variables. The macro \cdf{with-slots} invokes the function
\cdf{slot-value} to access the specified slots.
The macro \cdf{with-accessors} can be used to establish a lexical
environment in which specified slots are lexically available through
their accessors as if they were variables. The macro
\cdf{with-accessors} invokes the appropriate accessors to access the
specified slots. Any accessors specified by \cdf{with-accessors} must
already have been defined before they are used.
\subsection{Inheritance}
\label{Inheritance-SECTION}
A class can inherit methods, slots, and some \cdf{defclass} options
from its superclasses. The following sections describe the inheritance of
methods, the inheritance of slots and slot options, and the inheritance of
class options.
\subsubsection{Inheritance of Methods}
\label{Inheritance-of-Methods-SECTION}
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 whether the
method was created by using one of the method-defining forms or by
using one of the \cdf{defclass} options that causes methods to be
generated automatically.
The inheritance of methods is described in detail in
section~\ref{Method-Selection-and-Combination-SECTION}.
\subsubsection{Inheritance of Slots and Slot Options}
\label{Inheritance-of-Slots-and-Slot-Options-SECTION}
The set of names of all slots accessible in an instance of a class
\emph{C} is the union of the sets of names of slots defined by \emph{C} and its
superclasses. The \emph{structure} of an instance is the set of names
of local slots in that instance.
In the simplest case, only one class among \emph{C} and its superclasses
defines a slot with a given slot name. If a slot is defined by a
superclass of \emph{C}, the slot is said to be \emph{inherited}. The
characteristics of the slot are determined by the
slot specifier of the defining class. Consider the defining class for
a slot \emph{S}. If the value of the \cd{:allocation} slot
option is \cd{:instance}, then \emph{S} is a local slot and each instance
of \emph{C} has its own slot named \emph{S} that stores its own value. If the
value of the \cd{:allocation} slot option is \cd{:class}, then \emph{S}
is a shared slot, the class that defined \emph{S} stores the value, and all
instances of \emph{C} can access that single slot. If the
\cd{:allocation} slot option is omitted, \cd{:instance} is used.
In general, more than one class among \emph{C} and its superclasses can
define a slot with a given name. In such cases, only one slot with
the given name is accessible in an instance of \emph{C}, and
the characteristics of that slot are a combination of the several slot
specifiers, computed as follows:
\begin{itemize}
\item All the slot specifiers for a given slot name are ordered
from most specific to least specific, according to the order in \emph{C}'s
class precedence list of the classes that define them. All references
to the specificity of slot specifiers immediately following refer to this
ordering.
\item The allocation of a slot is controlled by the most specific
slot specifier. If the most specific slot specifier does not contain an
\cd{:allocation} slot option, \cd{:instance} is used. Less specific
slot specifiers do not affect the allocation.
\item The default initial value form for a
slot is the value of the \cd{:initform} slot option in the most
specific slot specifier that contains one. If no slot specifier
contains an \cd{:initform} slot option, the slot has no default
initial value form.
\item The contents of a slot will always be of type
\cd{(and $T_1$ $\ldots$ $T_{n}$)}
where $T_1, \ldots, T_{n}$ are
the values of the \cd{:type} slot options contained in all of the slot
specifiers. If no slot specifier contains the \cd{:type} slot option, the
contents of the slot will always be of type \cdf{t}. The result
of attempting to store in a slot
a value that does not satisfy the type of the slot is undefined.
\item The set of initialization arguments that initialize a given
slot is the union of the initialization arguments declared in the
\cd{:initarg} slot options in all the slot specifiers.
\item The documentation string for a slot is the value of the
\cd{:documentation} slot option in the most specific slot specifier
that contains one. If no slot specifier contains a
\cd{:documentation} slot option, the slot has no documentation string.
\end{itemize}
A consequence of the allocation rule is that a shared slot can be
shadowed. For example, if a class $C_1$ defines a slot named \emph{S}
whose value for the \cd{:allocation} slot option is \cd{:class},
that slot is accessible in instances of $C_1$ and all of its
subclasses. However, if $C_2$ is a subclass of $C_1$ and also
defines a slot named \emph{S}, $C_1$'s slot is not shared
by instances of $C_2$ and its subclasses. When a class
$C_1$ defines a shared slot, any subclass $C_2$ of $C_1$ will share this single
slot unless the \cdf{defclass} form for
$C_2$ specifies a slot of the same name or there is a superclass
of $C_2$ that precedes $C_1$ in the class precedence list of
$C_2$ that defines a slot of the same name.
A consequence of the type rule is that the value of a slot satisfies
the type constraint of each slot specifier that contributes to that
slot. Because the result of attempting to store in a slot a value
that does not satisfy the type constraint for the slot is undefined,
the value in a slot might fail to satisfy its type constraint.
The \cd{:reader}, \cd{:writer}, and \cd{:accessor} slot options
create methods rather than define the characteristics of a slot.
Reader and writer methods are inherited in the sense described in
section~\ref{Inheritance-of-Methods-SECTION}.
Methods that access slots use only the name of the slot and the type
of the slot's value. Suppose a superclass provides a method that
expects to access a shared slot of a given name, and a subclass defines
a local slot with the same name. If the method provided by the
superclass is used on an instance of the subclass, the method accesses
the local slot.
\subsubsection{Inheritance of Class Options}
The \cd{:default-initargs} class option is inherited. The set of
defaulted initialization arguments for a class is the union of the
sets of initialization arguments specified in the
\cd{:default-initargs} class options of the class and its superclasses.
When more than one default initial value form is supplied for a given
initialization argument, the default initial value form that is used
is the one supplied by the class that is most specific according to
the class precedence list.
If a given \cd{:default-initargs} class option specifies an
initialization argument of the same name more than once, an
error is signaled.
\subsubsection{Examples}
\begin{lisp}
(defclass C1 () \\*
~~((S1 :initform 5.4 :type number) \\*
~~~(S2 :allocation :class))) \\
\\
(defclass C2 (C1) \\*
~~((S1 :initform 5 :type integer)\\*
~~~(S2 :allocation :instance)\\*
~~~(S3 :accessor C2-S3)))
\end{lisp}
Instances of the class \cd{C1} have a local slot named \cd{S1}, whose default
initial value is 5.4 and whose value should always be a number.
The class \cd{C1} also has a shared slot named \cd{S2}.
There is a local slot named \cd{S1} in instances of \cd{C2}. The
default initial value of \cd{S1} is 5. The value of \cd{S1} will be
of type \cd{(and integer number)}. There are also local slots named
\cd{S2} and \cd{S3} in instances of \cd{C2}. The class \cd{C2}
has a method for \cd{C2-S3} for reading the value of slot \cd{S3};
there is also a method for \cd{(setf C2-S3)} that writes the
value of \cd{S3}.
\subsection{Integrating Types and Classes}
\label{Integrating-Types-and-Classes-SECTION}
The \CLOS\ maps the space of classes into the Common Lisp type space.
Every class that has a proper name has a corresponding type with the same
name.
The proper name of every class is a valid type specifier. In
addition, every class object is a valid type specifier. Thus the
expression \cd{(typep \emph{object class})} evaluates to true if the
class of \emph{object} is \emph{class} itself or a subclass of \emph{class}.
The evaluation of the expression \cd{(subtypep \emph{class1 class2})} returns
the values \cdf{t}~and~\cdf{t} if \emph{class1} is a
subclass of \emph{class2} or if they are the same class; otherwise it
returns the values \cdf{nil}~and~\cdf{t}. If \emph{I} is an instance of some class
\emph{C} named \emph{S} and \emph{C} is an instance of \cdf{standard-class}, the
evaluation of the expression \cd{(type-of \emph{I})} will return \emph{S} if
\emph{S} is the proper name of \emph{C}; if \emph{S} is not the proper
name of \emph{C}, the expression \cd{(type-of \emph{I})} will
return \emph{C}.
Because the names of classes and class objects are type specifiers, they may
be used in the special operator \cdf{the} and in type declarations.
Many but not all of the predefined Common Lisp type specifiers have a
corresponding class with the same proper name as the type. These type
specifiers are listed in table~\ref{CLOS-PRECEDENCE-TABLE}. For example, the
type \cdf{array} has a corresponding class named \cdf{array}. No type specifier
that is a list, such as \cd{(vector double-float 100)}, has a corresponding
class. The form \cdf{deftype} does not create any classes.
Each class that corresponds to a predefined Common Lisp type specifier
can be implemented in one of three ways, at the discretion of each
implementation. It can be a \emph{standard class} (of the kind
defined by \cdf{defclass}), a \emph{structure class} (defined
by \cdf{defstruct}), or a \emph{built-in class} (implemented in
a special, non-extensible way).
A built-in class is one whose instances have restricted capabilities or
special representations. Attempting to use \cdf{defclass} to define
subclasses of a built-in class signals an error. Calling
\cdf{make-instance} to create an instance of a built-in class signals an error.
Calling \cdf{slot-value} on an instance of a built-in class signals an
error. Redefining a built-in class or using \cdf{change-class} to change
the class of an instance to or from a built-in class signals an error.
However, built-in classes can be used as parameter specializers in
methods.
It is possible to determine whether a class is a built-in class by
checking the metaclass. A standard class is an instance of
\cdf{standard-class}, a built-in class is an instance of
\cdf{built-in-class}, and a structure class is an instance of
\cdf{structure-class}.
Each structure type created by \cdf{defstruct} without using the
\cd{:type} option has a corresponding class. This class is an instance of
\cdf{structure-class}.
The \cd{:include} option of \cdf{defstruct} creates a direct
subclass of the class that corresponds to the included structure.
The purpose of specifying that many of the standard Common Lisp type
specifiers have a corresponding class is to enable users to write methods that
discriminate on these types.
Method selection requires that a class precedence list can be
determined for each class.
The hierarchical relationships among the Common Lisp type specifiers
are mirrored by relationships among the classes corresponding to those
types. The existing type hierarchy is used for determining the
class precedence list for each class that corresponds to a predefined
Common Lisp type.
%In some cases, the first edition
%did not specify a local precedence order for two supertypes of a
%given type specifier. For example, \cdf{null} is a subtype of both
%\cdf{symbol} and \cdf{list}, but the first edition
%did not specify whether \cdf{symbol} is more specific or less
%specific than \cdf{list}. The CLOS specification defines those
%relationships for all such classes.
Table~\ref{CLOS-PRECEDENCE-TABLE} lists the set of classes required by the \OS\
that correspond to predefined Common Lisp type specifiers. The
superclasses of each such class are presented in order from most
specific to most general, thereby defining the class precedence list
for the class. The local precedence order for each class that
corresponds to a Common Lisp type specifier can be derived from this
table.
Individual implementations may be extended to define other type
specifiers to have a corresponding class. Individual implementations
can be extended to add other subclass relationships and to add other
elements to the class precedence lists in the above table as long as
they do not violate the type relationships and disjointness
requirements specified in section~\ref{DATA-TYPE-RELATIONSHIPS}.
A standard class defined with no direct superclasses is guaranteed to
be disjoint from all of the classes in the table, except for the
class named \cdf{t}.
[At this point the original CLOS report specified that certain Common Lisp
types were to appear in table~\ref{CLOS-PRECEDENCE-TABLE} if and only if
X3J13 voted to make them disjoint from
\cdf{cons}, \cdf{symbol}, \cdf{array}, \cdf{number}, and \cdf{character}.
X3J13 voted to do so in June 1988
\issue{DATA-TYPES-HIERARCHY-UNDERSPECIFIED}. I have added these types
and their class precedence lists to the table; the new types are indicated
by asterisks.---GLS]
\begin{table}[t]
\caption{Class Precedence Lists for Predefined Types}
\label{CLOS-PRECEDENCE-TABLE}
\begin{flushleft}
\cf
\begin{tabular}{@{}ll@{}}
\textrm{Predefined Common Lisp Type}&\textrm{Class Precedence List for Corresponding Class} \\
\hlinesp
array&(array t)\\
bit-vector&(bit-vector vector array sequence t)\\
character&(character t)\\
complex&(complex number t)\\
cons&(cons list sequence t)\\
float&(float number t)\\
function \textrm{*}&(function t) \\
hash-table \textrm{*}&(hash-table t) \\
integer&(integer rational number t)\\
list&(list sequence t)\\
null&(null symbol list sequence t)\\
number&(number t)\\
package \textrm{*}&(package t) \\
pathname \textrm{*}&(pathname t) \\
random-state \textrm{*}&(random-state t) \\
ratio&(ratio rational number t)\\
rational&(rational number t)\\
readtable \textrm{*}&(readtable t) \\
sequence&(sequence t)\\
stream \textrm{*}&(stream t) \\
string&(string vector array sequence t)\\
symbol&(symbol t)\\
t&(t)\\
vector&(vector array sequence t)
\end{tabular}
\end{flushleft}
[An asterisk indicates a type added to this table as a consequence
of a portion of the CLOS specification that was conditional on X3J13 voting
to make that type disjoint from certain other built-in types
\issue{DATA-TYPES-HIERARCHY-UNDERSPECIFIED}.---GLS]
\end{table}
\subsection{Determining the Class Precedence List}
\label{Determining-the-Class-Precedence-List-SECTION}
The \cdf{defclass} form for a class provides a total ordering on that
class and its direct superclasses. This ordering is called the \emph{local
precedence order}. It is an ordered list of the class and its
direct superclasses. The \emph{class precedence list} for a
class \emph{C} is a total ordering on \emph{C} and its superclasses that is consistent
with the local precedence orders for \emph{C} and its superclasses.
A class precedes its direct superclasses, and a
direct superclass precedes all other direct superclasses specified to
its right in the superclasses list of the \cdf{defclass} form. For
every class \emph{C}, define
$$R_C=\{(C,C_1),(C_1,C_2),\ldots,(C_{n-1},C_{n})\}$$
where $C_1,\ldots,C_{n}$ are
the direct superclasses of \emph{C} in the order in which
they are mentioned in the \cdf{defclass} form. These ordered pairs
generate the total ordering on the class \emph{C} and its direct
superclasses.
Let $S_C$ be the set of \emph{C} and its superclasses. Let \emph{R} be
$$R=\bigcup_{\textstyle c\in {S_{C}}} R_{c}$$
The set \emph{R} may or may not generate a partial ordering, depending on
whether the $R_{c}$, $c\in S_{C}$,
are consistent; it is assumed
that they are consistent and that \emph{R} generates a partial ordering.
When the $R_{c}$ are not consistent, it is said that \emph{R} is inconsistent.
To compute the class precedence list for~\emph{C},
topologically sort the elements of $S_{C}$ with respect to the
partial ordering generated by \emph{R}. When the topological
sort must select a class from a set of two or more classes, none of
which are preceded by other classes with respect to~\emph{R},
the class selected is chosen deterministically, as described below.
If \emph{R} is inconsistent, an error is signaled.
\subsubsection{Topological Sorting}
\href{http://ru.wikipedia.org/wiki/Топологическая_сортировка}{Топологическая
сортировка}
Topological sorting proceeds by finding a class \emph{C} in~$S_{C}$ such
that no other class precedes that element according to the elements
in~\emph{R}. The class \emph{C} is placed first in the result.
Remove \emph{C} from $S_{C}$, and remove all pairs of the form
$(C,D)$,
$D\in S_{C}$, from \emph{R}. Repeat the process, adding
classes with no predecessors to the end of the result. Stop when no
element can be found that has no predecessor.
If $S_{C}$ is not empty and the process has stopped, the set \emph{R} is
inconsistent. If every class in the finite set of classes is preceded
by another, then \emph{R} contains a loop. That is, there is a chain of
classes $C_1,\ldots,C_{n}$
such that $C_{i}$ precedes
$C_{i+1}$, $1\leq i<n$,
and $C_{n}$ precedes $C_1$.
Sometimes there are several classes from $S_{C}$ with no
predecessors. In this case select the one that has a direct
subclass rightmost in the class precedence list computed so far.
%%%%%%% RPG said to take out the following sentence 10/5/89.
%Because a direct superclass precedes all other direct superclasses to
%its right, there can be only one such candidate class.
If there is no
such candidate class, \emph{R} does not generate a partial ordering --- the
$R_{c}$, $c\in S_{C}$, are inconsistent.
In more precise terms, let $\{N_1,\ldots,N_{m}\}$,
$m\geq 2$, be
the classes from $S_{C}$ with no predecessors. Let $(C_1\ldots C_{n})$, $n\geq
1$, be the class precedence list
constructed so far. $C_1$ is the most specific class, and $C_n$ is the least
specific. Let $1\leq j\leq n$ be the largest number
such that there exists an \emph{i} where $1\leq i\leq m$ and
$N_{i}$
is a direct superclass of $C_{j}$;
$N_{i}$ is placed next.
The effect of this rule for selecting from a set of classes with no
predecessors is that classes in a simple superclass chain are
adjacent in the class precedence list and that classes in each
relatively separated subgraph are adjacent in the class
precedence list. For example, let $T_1$ and $T_2$ be subgraphs
whose only element in common is the class \emph{J}. Suppose
that no superclass of \emph{J} appears in either $T_1$ or $T_2$.
Let $C_1$ be the bottom of $T_1$; and let $C_2$ be the
bottom of $T_2$. Suppose \emph{C} is a class whose direct superclasses
are $C_1$ and $C_2$ in that order; then the class precedence
list for \emph{C} will start with \emph{C} and will be followed by all classes
in $T_1$ except \emph{J}. All the classes of $T_2$ will be next.
The class \emph{J} and its superclasses will appear last.
\subsubsection{Examples}
This example determines a class precedence list for the
class \cdf{pie}. The following classes are defined:
\begin{lisp}
(defclass pie (apple cinnamon) ()) \\*
(defclass apple (fruit) ()) \\*
(defclass cinnamon (spice) ()) \\
(defclass fruit (food) ()) \\
(defclass spice (food) ()) \\*
(defclass food () ())
\end{lisp}
\begin{flushleft}
The set
S=\{\cdf{pie},
\cdf{apple},
\cdf{cinnamon},
\cdf{fruit},
\cdf{spice},
\cdf{food},
\cdf{standard-object},
\cdf{t}\}.
The set R=\{(\cdf{pie},
\cdf{apple}),
(\cdf{apple},
\cdf{cinnamon}),
(\cdf{cinnamon},
\cdf{standard-object}),
(\cdf{apple},
\cdf{fruit}),
(\cdf{fruit},
\cdf{standard-object}),
(\cdf{cinnamon},
\cdf{spice}),
(\cdf{spice},
\cdf{standard-object}),
(\cdf{fruit},
\cdf{food}),
(\cdf{food},
\cdf{standard-object}),
(\cdf{spice},
\cdf{food}),
(\cdf{standard-object},
\cdf{t})\}.
The class \cdf{pie} is not preceded by anything, so it comes first;
the result so far is \cd{(pie)}. Remove \cdf{pie} from \emph{S} and pairs
mentioning \cdf{pie} from \emph{R} to get
S=\{\cdf{apple},
\cdf{cinnamon},
\cdf{fruit},
\cdf{spice},
\cdf{food},
\cdf{standard-object},
\cdf{t}\} and R=\{
(\cdf{apple},
\cdf{cinnamon}),
(\cdf{cinnamon},
\cdf{standard-object}),
(\cdf{apple},
\cdf{fruit}),
(\cdf{fruit},
\cdf{standard-object}),
(\cdf{cinnamon},
\cdf{spice}),
(\cdf{spice},
\cdf{standard-object}),
(\cdf{fruit},
\cdf{food}),
(\cdf{food},
\cdf{standard-object}),
(\cdf{spice},
\cdf{food}),
(\cdf{standard-object},
\cdf{t})\}.
The class \cdf{apple} is not preceded by anything, so it is next; the
result is \cd{(pie~apple)}. Removing \cdf{apple} and the relevant
pairs results in S=\{\cdf{cinnamon},
\cdf{fruit},
\cdf{spice},
\cdf{food},
\cdf{standard-object},
\cdf{t}\} and R=\{(\cdf{cinnamon},
\cdf{standard-object}),
(\cdf{fruit},