-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchap-7.texi
More file actions
5960 lines (4422 loc) · 228 KB
/
chap-7.texi
File metadata and controls
5960 lines (4422 loc) · 228 KB
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
@node Objects
@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
@section Object Creation and Initialization
The @term{generic function} @symbolref{make-instance, SYM} creates and returns a new
@term{instance} of a @term{class}. The first argument is a @term{class} or
the @term{name} of a @term{class}, and the remaining arguments form an
@newterm{initialization argument list}.
The initialization of a new @term{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 @term{instance}, filling @term{slots} with
values, and executing user-supplied @term{methods} that perform additional
initialization. Each step of @symbolref{make-instance, SYM} is implemented by a
@term{generic function} to provide a mechanism for customizing that step.
In addition, @symbolref{make-instance, SYM} is itself a @term{generic function}
and thus also can be customized.
The @OS{}@spc{}specifies system-supplied primary @term{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:
@itemize @bullet{}
@item Declaring a @term{symbol} to be an initialization argument
for a @term{slot}. An initialization argument is declared by using the
@kwd{initarg} slot option to @symbolref{defclass, SYM}. This provides a mechanism
for supplying a value for a @term{slot} in a call to @symbolref{make-instance, SYM}.
@item Supplying a default value form for an initialization argument.
Default value forms for initialization arguments are defined by using the
@kwd{default-initargs} class option to @symbolref{defclass, SYM}. If an
initialization argument is not explicitly provided
as an argument to @symbolref{make-instance, SYM}, the default value form is
evaluated in the lexical environment of the @symbolref{defclass, SYM} form that
defined it, and the resulting value is used as the value of the
initialization argument.
@item Supplying a default initial value form for a @term{slot}.
A default initial value form for a @term{slot} is defined by using the
@kwd{initform} slot option to @symbolref{defclass, SYM}. If no initialization
argument associated with that @term{slot} is given as an argument to
@symbolref{make-instance, SYM} or is defaulted by @kwd{default-initargs}, this
default initial value form is evaluated in the lexical environment of
the @symbolref{defclass, SYM} form that defined it, and the resulting value is
stored in the @term{slot}. The @kwd{initform} form for a
@term{local slot} may be used when creating an @term{instance}, when
updating an @term{instance} to conform to a redefined @term{class},
or when updating an @term{instance} to conform to the definition of a
different @term{class}. The @kwd{initform} form for a
@term{shared slot} may be used when defining or re-defining the @term{class}.
@item
Defining @term{methods} for @symbolref{initialize-instance, SYM} and
@symbolref{shared-initialize, SYM}. The slot-filling behavior described above is
implemented by a system-supplied primary @term{method} for
@symbolref{initialize-instance, SYM} which invokes @symbolref{shared-initialize, SYM}. The
@term{generic function} @symbolref{shared-initialize, SYM} implements the parts of
initialization shared by these four situations: when making an @term{instance},
when re-initializing an @term{instance}, when updating an @term{instance}
to conform to a redefined @term{class}, and when updating an @term{instance}
to conform to the definition of a different @term{class}. The system-supplied
primary @term{method} for @symbolref{shared-initialize, SYM} directly implements the
slot-filling behavior described above, and @symbolref{initialize-instance, SYM}
simply invokes @symbolref{shared-initialize, SYM}.
@end itemize
@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
@subsection Initialization Arguments
An initialization argument controls @term{object} creation and
initialization. It is often convenient to use keyword @term{symbols}
to name initialization arguments, but the @term{name} of an
initialization argument can be any @term{symbol}, including @nil{}. An
initialization argument can be used in two ways: to fill a @term{slot}
with a value or to provide an argument for an initialization
@term{method}. A single initialization argument can be used for both
purposes.
An @term{initialization argument list} is a
@term{property list} of
initialization argument names and values.
Its structure is identical
to a @term{property list} and also
to the portion of an argument list
processed for @keyref{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 @symbolref{make-instance, SYM} (after the first
argument) form an @term{initialization argument list}.
An initialization argument can be associated with a @term{slot}. If
the initialization argument has a value in the @term{initialization
argument list}, the value is stored into the @term{slot} of the newly
created @term{object}, overriding any @kwd{initform} form associated
with the @term{slot}. A single initialization argument can initialize
more than one @term{slot}. An initialization argument that initializes
a @term{shared slot} stores its value into the @term{shared slot},
replacing any previous value.
An initialization argument can be associated with a @term{method}. When
an @term{object} is created and a particular initialization argument is
supplied, the @term{generic functions} @symbolref{initialize-instance, SYM},
@symbolref{shared-initialize, SYM}, and @symbolref{allocate-instance, SYM} 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
@term{initialization argument list}, the @term{method}'s
@term{lambda list} supplies a default value.
Initialization arguments are used in four situations: when making an
@term{instance}, when re-initializing an @term{instance}, when updating
an @term{instance} to conform to a redefined @term{class}, and when
updating an @term{instance} to conform to the definition of a different
@term{class}.
Because initialization arguments are used to control the creation and
initialization of an @term{instance} of some particular @term{class},
we say that an initialization argument is
``an initialization argument for'' that @term{class}.
@node Declaring the Validity of Initialization Arguments
@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 @term{method} for @symbolref{make-instance, SYM} defined for
@theclass{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.
@itemize @bullet{}
@item
Initialization arguments that fill @term{slots} are declared as valid
by the @kwd{initarg} slot option to @symbolref{defclass, SYM}. The
@kwd{initarg} slot option is inherited from @term{superclasses}. Thus
the set of valid initialization arguments that fill @term{slots} for a
@term{class} is the union of the initialization arguments that fill
@term{slots} declared as valid by that @term{class} and its
@term{superclasses}. Initialization arguments that fill @term{slots}
are valid in all four contexts.
@item
Initialization arguments that supply arguments to @term{methods} are
declared as valid by defining those @term{methods}. The keyword name of
each keyword parameter specified in the @term{method}'s
@term{lambda list} becomes an initialization argument for all @term{classes}
for which the @term{method} is applicable.
The presence of @allowotherkeys{} in the
@term{lambda list} of an applicable method disables validity checking of
initialization arguments.
Thus @term{method} inheritance
controls the set of valid initialization arguments that supply arguments
to @term{methods}. The @term{generic functions} for which @term{method}
definitions serve to declare initialization arguments valid are as
follows:
@itemize --
@item
Making an @term{instance} of a @term{class}:
@symbolref{allocate-instance, SYM}, @symbolref{initialize-instance, SYM}, and
@symbolref{shared-initialize, SYM}. Initialization arguments declared as valid
by these @term{methods} are valid when making
an @term{instance} of a @term{class}.
@item Re-initializing an @term{instance}:
@symbolref{reinitialize-instance, SYM} and @symbolref{shared-initialize, SYM}.
Initialization arguments declared as valid by these @term{methods} are
valid when re-initializing an @term{instance}.
@item Updating an @term{instance} to conform to a redefined @term{class}:
@symbolref{update-instance-for-redefined-class, SYM} and @symbolref{shared-initialize, SYM}.
Initialization arguments declared as valid by these @term{methods} are
valid when updating an @term{instance} to conform to a redefined @term{class}.
@item Updating an @term{instance} to conform to the definition of a
different @term{class}:
@symbolref{update-instance-for-different-class, SYM} and @symbolref{shared-initialize, SYM}.
Initialization arguments declared as valid by these @term{methods} are
valid when updating an @term{instance} to conform to the definition
of a different @term{class}.
@end itemize
@end itemize
The set of valid initialization arguments for a @term{class} is the set of
valid initialization arguments that either fill @term{slots} or supply
arguments to @term{methods}, along with the predefined initialization
argument @kwd{allow-other-keys}. The default value for
@kwd{allow-other-keys} is @nil{}.
Validity checking of initialization arguments is disabled if the value of
the initialization argument @kwd{allow-other-keys} is @term{true}.
@node Defaulting of Initialization Arguments
@subsection Defaulting of Initialization Arguments
A default value @term{form} can be supplied for an initialization
argument by using the @kwd{default-initargs} @term{class} option. If an
initialization argument is declared valid by some particular @term{class},
its default value form might be specified by a different @term{class}.
In this case @kwd{default-initargs} is used to supply a default value
for an inherited initialization argument.
The @kwd{default-initargs} option is used only to provide default
values for initialization arguments; it does not declare a @term{symbol}
as a valid initialization argument name. Furthermore,
the @kwd{default-initargs} option is used only to provide default values for
initialization arguments when making an @term{instance}.
The argument to the @kwd{default-initargs} class
option is a list of
alternating initialization argument names and @term{forms}.
Each @term{form} is the
default value form for the corresponding initialization
argument. The default value @term{form} of an initialization
argument is used and evaluated only if that initialization argument
does not appear in the arguments to @symbolref{make-instance, SYM} and is not
defaulted by a more specific @term{class}. The default value @term{form} is
evaluated in the lexical environment of the @symbolref{defclass, SYM} form that
supplied it; the resulting value is used as the initialization
argument's value.
The initialization arguments supplied to @symbolref{make-instance, SYM} are combined
with defaulted initialization arguments to produce a
@term{defaulted initialization argument list}. A
@term{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
@term{class precedence list} of the @term{classes} that supplied the default values.
There is a distinction between the purposes of the
@kwd{default-initargs} and the @kwd{initform} options with respect to the
initialization of @term{slots}. The @kwd{default-initargs}
class option
provides a mechanism for the user to give a default value @term{form}
for an initialization argument without knowing whether the
initialization argument initializes a @term{slot}
or is passed to a @term{method}.
If that initialization argument is not explicitly supplied in a call
to @symbolref{make-instance, SYM}, the default value @term{form} is used, just
as if it had been supplied in the call. In contrast, the
@kwd{initform} slot option provides a mechanism for the user to give a
default initial value form for a @term{slot}. An @kwd{initform} form is
used to initialize a @term{slot} only if no initialization argument
associated with that @term{slot} is given as an argument to
@symbolref{make-instance, SYM} or is defaulted by @kwd{default-initargs}.
@cindex order of evaluation
@cindex evaluation order
The order of evaluation of default value @term{forms} for initialization
arguments and the order of evaluation of @kwd{initform} forms are
undefined. If the order of evaluation is important,
@symbolref{initialize-instance, SYM} or @symbolref{shared-initialize, SYM} @term{methods}
should be used
instead.
@node Rules for Initialization Arguments
@subsection Rules for Initialization Arguments
The @kwd{initarg} slot option may be specified more than
once for a given @term{slot}.
The following rules specify when initialization arguments may be
multiply defined:
@itemize @bullet{}
@item A given initialization argument can be used to
initialize more than one @term{slot} if the same initialization argument name
appears in more than one @kwd{initarg} slot option.
@item A given initialization argument name can appear
in the @term{lambda list} of more than one initialization @term{method}.
@item A given initialization argument name can
appear both in an @kwd{initarg} slot option and
in the @term{lambda list}
of an initialization @term{method}.
@end itemize
@reviewer{The next three paragraphs could be replaced by ``If two or more
initialization arguments that initialize the same slot appear in the
@term{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
@term{slot} are given in the arguments to @symbolref{make-instance, SYM}, the
leftmost of these initialization arguments in the
@term{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 @term{slot} have default values and none is given explicitly in the
arguments to @symbolref{make-instance, SYM}, the initialization argument that
appears in a @kwd{default-initargs} class option in the most specific
of the @term{classes} supplies the value. If a single
@kwd{default-initargs} class option specifies two or more initialization
arguments that initialize the same @term{slot} and none is given
explicitly in the arguments to @symbolref{make-instance, SYM}, the leftmost in
the @kwd{default-initargs} class option supplies the value, and the
values of the remaining default value @term{forms} are ignored.
Initialization arguments given explicitly in the arguments to
@symbolref{make-instance, SYM} appear to the left of defaulted initialization
arguments. Suppose that the classes @mat{C@sub{1}} and @mat{C@sub{2}} supply the
values of defaulted initialization arguments for different @term{slots},
and suppose that @mat{C@sub{1}} is more specific than @mat{C@sub{2}}; then the
defaulted initialization argument whose value is supplied by @mat{C@sub{1}}
is to the left of the defaulted initialization argument whose value is
supplied by @mat{C@sub{2}} in the @term{defaulted initialization argument
list}. If a single @kwd{default-initargs} class option supplies the
values of initialization arguments for two different @term{slots}, the
initialization argument whose value is specified farther to the left in
the @kwd{default-initargs} class option appears farther to the left in
the @term{defaulted initialization argument list}.
@reviewer{Barmar: End of claim made three paragraphs back.}
If a @term{slot} has both an @kwd{initform} form and an
@kwd{initarg} slot option, and the initialization argument is defaulted
using @kwd{default-initargs} or is supplied to @symbolref{make-instance, SYM},
the captured @kwd{initform} form is neither used nor evaluated.
The following is an example of the above rules:
@lisp
(defclass q () ((x :initarg a)))
(defclass r (q) ((x :initarg b))
(:default-initargs a 1 b 2))
@end lisp
@initargs{}
@node Shared-Initialize
@subsection Shared-Initialize
The @term{generic function} @symbolref{shared-initialize, SYM} is used to fill the
@term{slots}
of an @term{instance}
using initialization arguments and @kwd{initform}
forms when an @term{instance} is created, when an
@term{instance} is re-initialized,
when an @term{instance}
is updated to conform to a redefined @term{class}, and when
an @term{instance} is updated to conform to a different @term{class}.
It uses
standard @term{method} combination. It takes the following arguments: the
@term{instance} to be initialized, a
specification of a set of @term{names} of @term{slots}
@term{accessible} in that @term{instance}, and any number of initialization
arguments. The arguments after the first two must form an
@term{initialization argument list}.
The second argument to @symbolref{shared-initialize, SYM} may be one of the following:
@itemize @bullet{}
@item It can be a (possibly empty) @term{list} of @term{slot} names,
which specifies the set of those @term{slot} names.
@item It can be the symbol @symbolref{t, SC}, which specifies the set of all of the @term{slots}.
@end itemize
There is a system-supplied primary @term{method} for @symbolref{shared-initialize, SYM}
whose first @term{parameter specializer} is @theclass{standard-object}.
This @term{method} behaves as follows on each @term{slot},
whether shared or local:
@itemize @bullet{}
@item If an initialization argument in the
@term{initialization argument list} specifies a value for that @term{slot},
that value is stored
into the @term{slot}, even if a value has already been stored in the @term{slot}
before the @term{method} is run.
The affected @term{slots} are independent of which
@term{slots} are indicated by the second argument to @symbolref{shared-initialize, SYM}.
@item Any @term{slots}
indicated by the second argument that are still
unbound at this point are initialized according to their
@kwd{initform} forms. For any such @term{slot}
that has an @kwd{initform} form,
that @term{form} is evaluated in the
lexical environment of its defining
@symbolref{defclass, SYM} form and the result is stored into the @term{slot}.
For example,
if a @term{before method} stores a value in the
@term{slot}, the @kwd{initform} form will not be used to supply a value
for the @term{slot}. If
the second argument specifies a @term{name} that does not correspond to any
@term{slots} @term{accessible}
in the @term{instance}, the results are unspecified.
@item The rules mentioned in @ref{Rules for Initialization Arguments} are obeyed.
@end itemize
The generic function @symbolref{shared-initialize, SYM} is called by the
system-supplied primary @term{methods}
for @symbolref{reinitialize-instance, SYM},
@symbolref{update-instance-for-different-class, SYM},
@symbolref{update-instance-for-redefined-class, SYM}, and
@symbolref{initialize-instance, SYM}. Thus, @term{methods} can be written for
@symbolref{shared-initialize, SYM} to specify actions that should be taken in all of
these contexts.
@node Initialize-Instance
@subsection Initialize-Instance
The @term{generic function} @symbolref{initialize-instance, SYM} is called by
@symbolref{make-instance, SYM} to initialize a newly created @term{instance}.
It uses @term{standard method combination}. @term{Methods} for
@symbolref{initialize-instance, SYM} can be defined in order to perform any
initialization that cannot be achieved
simply by supplying initial values for @term{slots}.
During initialization, @symbolref{initialize-instance, SYM} is invoked
after the following actions have been taken:
@itemize @bullet{}
@item The @term{defaulted initialization argument list}
has been computed by combining the supplied @term{initialization argument list}
with any default initialization arguments for the @term{class}.
@item The validity of the @term{defaulted initialization argument list}
has been checked. If any of the initialization arguments has not
been declared as valid, an error is signaled.
@item A new @term{instance} whose @term{slots}
are unbound has been created.
@end itemize
The generic function @symbolref{initialize-instance, SYM} is called with the
new @term{instance} and the defaulted initialization arguments. There is
a system-supplied primary @term{method} for @symbolref{initialize-instance, SYM}
whose @term{parameter specializer} is @theclass{standard-object}. This
@term{method} calls the generic function
@symbolref{shared-initialize, SYM} to fill in
the @term{slots} according to the initialization arguments and the
@kwd{initform} forms for the @term{slots}; the generic function
@symbolref{shared-initialize, SYM} is called with the following arguments: the @term{instance},
@symbolref{t, SC}, and the defaulted initialization arguments.
Note that @symbolref{initialize-instance, SYM} provides the
@term{defaulted initialization argument list} in its call to @symbolref{shared-initialize, SYM},
so the first step performed by the system-supplied primary @term{method} for
@symbolref{shared-initialize, SYM} takes into account both the initialization
arguments provided in the call to @symbolref{make-instance, SYM} and the
@term{defaulted initialization argument list}.
@term{Methods} for @symbolref{initialize-instance, SYM} can be defined to specify
actions to be taken when an @term{instance} is initialized.
If only @term{after methods} for @symbolref{initialize-instance, SYM} are defined, they will be
run after the system-supplied primary @term{method} for initialization and
therefore will not interfere with the default behavior of
@symbolref{initialize-instance, SYM}.
The @OS{}@spc{}provides two @term{functions} that are useful in the bodies of
@symbolref{initialize-instance, SYM} methods. @Thefunction{slot-boundp}
returns a @i{generic boolean} value that indicates whether a specified @term{slot} has a
value; this provides a mechanism for writing @term{after methods} for
@symbolref{initialize-instance, SYM} that initialize @term{slots} only if they have
not already been initialized. @Thefunction{slot-makunbound}
causes the @term{slot} to have no value.
@node Definitions of Make-Instance and Initialize-Instance
@subsection Definitions of Make-Instance and Initialize-Instance
The generic function @symbolref{make-instance, SYM} behaves as if it were defined as
follows, except that certain optimizations are permitted:
@lisp
(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 lisp
The elided code in the definition of @symbolref{make-instance, SYM}
augments the @f{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 @term{slot} nor supplied an argument
to an applicable @term{method}.
The generic function @symbolref{initialize-instance, SYM} behaves as if it were
defined as follows, except that certain optimizations are permitted:
@lisp
(defmethod initialize-instance ((instance standard-object) &rest initargs)
(apply #'shared-initialize instance t initargs)))
@end lisp
These procedures can be customized.
Customizing at the Programmer Interface level includes using the
@kwd{initform}, @kwd{initarg}, and @kwd{default-initargs} options to
@symbolref{defclass, SYM}, as well as defining @term{methods}
for @symbolref{make-instance, SYM},
@symbolref{allocate-instance, SYM},
and @symbolref{initialize-instance, SYM}. It is also possible to define
@term{methods} for @symbolref{shared-initialize, SYM}, which would be invoked by the
generic functions @symbolref{reinitialize-instance, SYM},
@symbolref{update-instance-for-redefined-class, SYM},
@symbolref{update-instance-for-different-class, SYM}, and
@symbolref{initialize-instance, SYM}.
The meta-object level supports additional
customization.
Implementations are permitted to make certain optimizations to
@symbolref{initialize-instance, SYM} and @symbolref{shared-initialize, SYM}.
The description of @symbolref{shared-initialize, SYM} in Chapter@tie{}7 mentions the
possible optimizations.
@node Changing the Class of an Instance
@section Changing the Class of an Instance
@Thefunction{change-class} can be used to change the @term{class}
of an @term{instance} from its current class, @mat{C@sub{@hbox{@prmseven{from}}}},
to a different class, @mat{C@sub{@hbox{@prmseven{to}}}}; it changes the
structure of the @term{instance} to conform to the definition of the class
@mat{C@sub{@hbox{@prmseven{to}}}}.
Note that changing the @term{class} of an @term{instance} may cause
@term{slots} to be added or deleted. Changing the @term{class} of an
@term{instance} does not change its identity as defined by the
@symbolref{eq, SYM} function.
When @symbolref{change-class, SYM} is invoked on an @term{instance}, a two-step
updating process takes place. The first step modifies the structure of
the @term{instance} by adding new @term{local slots} and discarding
@term{local slots} that are not specified in the new version of the @term{instance}.
The second step initializes the newly added @term{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::
* Customizing the Change of Class of an Instance::
@end menu
@node Modifying the Structure of the Instance
@subsection Modifying the Structure of the Instance
In order to make the @term{instance} conform to the class
@mat{C@sub{@hbox{@prmseven{to}}}}, @term{local slots}
specified by the class @mat{C@sub{@hbox{@prmseven{to}}}}
that are not specified by the class @mat{C@sub{@hbox{@prmseven{from}}}} are added,
and @term{local slots} not specified by
the class @mat{C@sub{@hbox{@prmseven{to}}}} that are specified by the
class @mat{C@sub{@hbox{@prmseven{from}}}} are discarded.
The values of @term{local slots} specified by both the
class @mat{C@sub{@hbox{@prmseven{to}}}} and the class
@mat{C@sub{@hbox{@prmseven{from}}}} are retained.
If such a @term{local slot} was unbound, it remains unbound.
The values of @term{slots} specified as shared
in the class @mat{C@sub{@hbox{@prmseven{from}}}}
and as local in the class @mat{C@sub{@hbox{@prmseven{to}}}} are retained.
This first step of the update does not affect the values of any
@term{shared slots}.
@node Initializing Newly Added Local Slots
@subsection Initializing Newly Added Local Slots
The second step of the update initializes the newly added @term{slots} and
performs any other user-defined actions. This step is implemented by
the generic function @symbolref{update-instance-for-different-class, SYM}. The
generic function @symbolref{update-instance-for-different-class, SYM} is invoked
by @symbolref{change-class, SYM} after the first step of the update has been
completed.
The generic function @symbolref{update-instance-for-different-class, SYM} is
invoked on arguments computed by @symbolref{change-class, SYM}.
The first argument passed is a copy of the @term{instance} being updated
and is an @term{instance} of the class @mat{C@sub{@hbox{@prmseven{from}}}};
this copy has @term{dynamic extent} within the generic function @symbolref{change-class, SYM}.
The second argument is the @term{instance} as updated so far by @symbolref{change-class, SYM}
and is an @term{instance} of the class @mat{C@sub{@hbox{@prmseven{to}}}}.
The remaining arguments are an @term{initialization argument list}.
There is a system-supplied primary @term{method} for
@symbolref{update-instance-for-different-class, SYM} that has two parameter
specializers, each of which is @theclass{standard-object}. First
this @term{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 @symbolref{shared-initialize, SYM} with the following arguments:
the
new
@term{instance}, a list of @term{names} of the newly added
@term{slots}, and the
initialization arguments it received.
@node Customizing the Change of Class of an Instance
@subsection Customizing the Change of Class of an Instance
@term{Methods} for @symbolref{update-instance-for-different-class, SYM} may be defined
to specify actions to be taken when an @term{instance} is updated. If only
@term{after methods} for @symbolref{update-instance-for-different-class, SYM} are
defined, they will be run after the system-supplied primary @term{method} for
initialization and will not interfere with the default behavior of
@symbolref{update-instance-for-different-class, SYM}.
@term{Methods}
for @symbolref{shared-initialize, SYM} may be defined to customize @term{class}
redefinition. For more information, see @ref{Shared-Initialize}.
@node Reinitializing an Instance
@section Reinitializing an Instance
The generic function @symbolref{reinitialize-instance, SYM} may be used to change
the values of @term{slots} according to initialization arguments.
The process of reinitialization changes the values of some @term{slots} and
performs any user-defined actions. It does not modify the structure
of an @term{instance} to add or delete @term{slots},
and it does not use any @kwd{initform} forms to initialize @term{slots}.
The generic function @symbolref{reinitialize-instance, SYM} may be called
directly. It takes one required argument, the @term{instance}. It also
takes any number of initialization arguments to be used by @term{methods} for
@symbolref{reinitialize-instance, SYM} or for @symbolref{shared-initialize, SYM}. The
arguments after the required @term{instance} must form an
@term{initialization argument list}.
There is a system-supplied primary @term{method} for
@symbolref{reinitialize-instance, SYM} whose @term{parameter specializer} is
@theclass{standard-object}. First this @term{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
@symbolref{shared-initialize, SYM} with the following arguments: the @term{instance},
@nil{}, and the initialization arguments it received.
@menu
* Customizing Reinitialization::
@end menu
@node Customizing Reinitialization
@subsection Customizing Reinitialization
@term{Methods} for @symbolref{reinitialize-instance, SYM} may be defined to specify
actions to be taken when an @term{instance} is updated. If only
@term{after methods} for @symbolref{reinitialize-instance, SYM} are defined,
they will be run after the system-supplied primary @term{method} for
initialization and therefore will not interfere with the default behavior of
@symbolref{reinitialize-instance, SYM}.
@term{Methods} for @symbolref{shared-initialize, SYM} may be defined to customize
@term{class} redefinition. For more information, see @ref{Shared-Initialize}.
@node Meta-Objects
@section Meta-Objects
The implementation of the @OS{}@spc{}manipulates @term{classes}, @term{methods},
and @term{generic functions}. The @OS{}@spc{}contains a set of
@term{generic functions} defined by @term{methods} on @term{classes};
the behavior of those @term{generic functions} defines the behavior of
the @OS{}. The @term{instances} of the @term{classes} on which those
@term{methods} are defined are called meta-objects.
@menu
* Standard Meta-objects::
@end menu
@node Standard Meta-objects
@subsection Standard Meta-objects
The @OS{}@spc{}supplies a set of meta-objects, called standard meta-objects.
These include @theclass{standard-object} and
@term{instances} of the classes @symbolref{standard-method, SYM},
@symbolref{standard-generic-function, SYM}, and @symbolref{method-combination, SYM}.
@itemize @bullet{}
@editornote{KMP: This is said redundantly in the definition of STANDARD-METHOD.}
@item
@Theclass{standard-method} is the default @term{class} of
@term{methods} defined by the
@symbolref{defmethod, SYM} and
@symbolref{defgeneric, SYM} @term{forms}.
@item
@Theclass{standard-generic-function} is the default @term{class} of
@term{generic functions} defined by the forms
@symbolref{defmethod, SYM},
@symbolref{defgeneric, SYM},
and
@symbolref{defclass, SYM}.
@item The @term{class} named @symbolref{standard-object, SYM}
is an @term{instance} of @theclass{standard-class}
and is a @term{superclass} of every @term{class} that is an
@term{instance} of @symbolref{standard-class, SYM} except itself and
@symbolref{structure-class, SYM}.
@item Every @term{method} combination object is
an @term{instance} of a @subclassof{method-combination}.
@end itemize
@node Slots
@section Slots
@menu
* Introduction to Slots::
* Accessing Slots::
* Inheritance of Slots and Slot Options::
@end menu
@node Introduction to Slots
@subsection Introduction to Slots
An @term{object} @ofmetaclass{standard-class} has zero or more named
@term{slots}. The @term{slots} of an @term{object} are determined
by the @term{class} of the @term{object}. Each @term{slot} can hold
one value.
@reviewer{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 @term{name} of a @term{slot} is a @term{symbol} that is syntactically
valid for use as a variable name.
When a @term{slot} does not have a value, the @term{slot} is said to be
@term{unbound}. When an unbound @term{slot} is read,
@reviewer{Barmar: from an object whose metaclass is standard-class?}
the @term{generic function} @symbolref{slot-unbound, SYM} is invoked. The
system-supplied primary @term{method}
for @symbolref{slot-unbound, SYM}
on @term{class} @symbolref{t, SC} signals an error.
If @symbolref{slot-unbound, SYM} returns, its @term{primary value}
is used that time as the @term{value} of the @term{slot}.
The default initial value form for a @term{slot} is defined by
the @kwd{initform} slot option. When the @kwd{initform} form is used to
supply a value, it is evaluated in the lexical environment in which
the @symbolref{defclass, SYM} form was evaluated. The @kwd{initform} along with
the lexical environment in which the @symbolref{defclass, SYM} form was evaluated
is called a @term{captured initialization form}.
For more details, see @ref{Object Creation and Initialization}.
A @term{local slot} is defined to be a @term{slot} that is
@term{accessible}
to exactly one @term{instance},
namely the one in which the @term{slot} is allocated.
A @term{shared slot} is defined to be a @term{slot} that is visible to more than one
@term{instance} of a given @term{class} and its @term{subclasses}.
A @term{class} is said to define a @term{slot} with a given @term{name} when
the @symbolref{defclass, SYM} form for that @term{class} contains a @term{slot specifier} with
that @term{name}. Defining a @term{local slot} does not immediately create
a @term{slot}; it causes a @term{slot} to be created each time
an @term{instance} of the @term{class} is created.
Defining a @term{shared slot} immediately creates a @term{slot}.
The @kwd{allocation} slot option to @symbolref{defclass, SYM} controls the kind
of @term{slot} that is defined. If the value of the @kwd{allocation} slot
option is @kwd{instance}, a @term{local slot} is created. If the value of
@kwd{allocation} is @kwd{class}, a @term{shared slot} is created.
A @term{slot} is said to be @term{accessible} in an @term{instance}
of a @term{class} if
the @term{slot} is defined by the @term{class}
of the @term{instance} or is inherited from
a @term{superclass} of that @term{class}.
At most one @term{slot} of a given @term{name} can be
@term{accessible} in an @term{instance}.
A @term{shared slot} defined by a @term{class} is
@term{accessible} in all @term{instances}
of that @term{class}.
A detailed explanation of the inheritance of @term{slots} is given in
@ref{Inheritance of Slots and Slot Options}.
@node Accessing Slots
@subsection Accessing Slots
@term{Slots} can be @term{accessed} in two ways: by use of the primitive function
@symbolref{slot-value, SYM} and by use of @term{generic functions} generated by
the @symbolref{defclass, SYM} form.
@Thefunction{slot-value} can be used with any of the @term{slot}
names specified in the @symbolref{defclass, SYM} form to @term{access} a specific
@term{slot} @term{accessible} in an @term{instance} of the given @term{class}.
The macro @symbolref{defclass, SYM} provides syntax for generating @term{methods} to
read and write @term{slots}. If a reader @term{method} is requested,
a @term{method} is automatically generated for reading the value of the
@term{slot}, but no @term{method} for storing a value into it is generated.
If a writer @term{method} is requested, a @term{method} is automatically
generated for storing a value into the @term{slot}, but no @term{method}
for reading its value is generated. If an accessor @term{method} is
requested, a @term{method} for reading the value of the @term{slot} and a
@term{method} for storing a value into the @term{slot} are automatically
generated. Reader and writer @term{methods} are implemented using
@symbolref{slot-value, SYM}.
When a reader or writer @term{method} is specified for a @term{slot}, the
name of the @term{generic function} to which the generated @term{method}
belongs is directly specified. If the @term{name} specified for the writer
@term{method} is the symbol @f{name}, the @term{name} of the
@term{generic function} for writing the @term{slot} is the symbol
@f{name}, and the @term{generic function} takes two arguments: the new
value and the @term{instance}, in that order. If the @term{name} specified
for the accessor @term{method} is the symbol @f{name}, the @term{name} of
the @term{generic function} for reading the @term{slot} is the symbol
@f{name}, and the @term{name} of the @term{generic function} for writing
the @term{slot} is the list @f{(setf name)}.
A @term{generic function} created or modified by supplying @kwd{reader},
@kwd{writer}, or @kwd{accessor} @term{slot} options can be treated exactly
as an ordinary @term{generic function}.
Note that @symbolref{slot-value, SYM} can be used to read or write the value of a
@term{slot} whether or not reader or writer @term{methods} exist for that
@term{slot}. When @symbolref{slot-value, SYM} is used, no reader or writer
@term{methods} are invoked.
The macro @symbolref{with-slots, SYM} can be used to establish a
@term{lexical environment} in which specified @term{slots} are lexically
available as if they were variables. The macro @symbolref{with-slots, SYM}
invokes @thefunction{slot-value} to @term{access} the specified @term{slots}.
The macro @symbolref{with-accessors, SYM} can be used to establish a lexical
environment in which specified @term{slots} are lexically available through
their accessors as if they were variables. The macro @symbolref{with-accessors, SYM}
invokes the appropriate accessors to @term{access} the specified @term{slots}.
@node Inheritance of Slots and Slot Options
@subsection Inheritance of Slots and Slot Options
The set of the @term{names} of all @term{slots} @term{accessible}
in an @term{instance} of a @term{class} @mat{C} is the union of
the sets of @term{names} of @term{slots} defined by @mat{C} and its
@term{superclasses}. The structure of an @term{instance} is
the set of @term{names} of @term{local slots} in that @term{instance}.
In the simplest case, only one @term{class} among @mat{C} and its @term{superclasses}
defines a @term{slot} with a given @term{slot} name.
If a @term{slot} is defined by a @term{superclass} of @mat{C}@negthinspace{},
the @term{slot} is said to be inherited. The characteristics
of the @term{slot} are determined by the @term{slot specifier}
of the defining @term{class}.
Consider the defining @term{class} for
a slot @mat{S}@negthinspace{}. If the value of the @kwd{allocation}
slot
option is @kwd{instance}, then @mat{S} is a @term{local slot} and each
@term{instance}
of @mat{C} has its own @term{slot} named @mat{S} that stores its own value. If the
value of the @kwd{allocation} slot
option is @kwd{class}, then @mat{S}
is a @term{shared slot}, the @term{class}
that defined @mat{S} stores the value, and all
@term{instances} of @mat{C} can @term{access} that single @term{slot}.
If the @kwd{allocation} slot option is omitted, @kwd{instance} is used.
In general, more than one @term{class} among @mat{C} and its
@term{superclasses} can
define a @term{slot} with a given @term{name}.
In such cases, only one @term{slot} with
the given name is @term{accessible} in an @term{instance}
of @mat{C}@negthinspace{}, and
the characteristics of that @term{slot} are
a combination of the several @term{slot}
specifiers, computed as follows:
@itemize @bullet{}
@item All the @term{slot specifiers} for a given @term{slot} name
are ordered from most specific to least specific, according to the order in @mat{C}'s
@term{class precedence list} of the @term{classes} that define them. All references
to the specificity of @term{slot specifiers} immediately below refers to this
ordering.
@item The allocation of a @term{slot} is controlled by the most
specific @term{slot specifier}. If the most specific @term{slot specifier}
does not contain an @kwd{allocation} slot option, @kwd{instance} is used.
Less specific @term{slot specifiers} do not affect the allocation.
@item The default initial value form for a @term{slot}
is the value of the @kwd{initform} slot option in the most specific
@term{slot specifier} that contains one. If no @term{slot specifier}
contains an @kwd{initform} slot option, the @term{slot}
has no default initial value form.
@item The contents of a @term{slot} will always be of type
@f{(and @mat{T@sub{1}} @mat{@ldots{}} @mat{T@sub{n}})} where @mat{T@sub{1}@ldots{} T@sub{n}} are
the values of the @kwd{type} slot options contained in all of the
@term{slot specifiers}. If no @term{slot specifier} contains the
@kwd{type} slot option, the contents of the @term{slot} will always be
@oftype{t}. The consequences of attempting to store in a @term{slot}
a value that does not satisfy the @term{type} of the @term{slot} are undefined.
@item The set of initialization arguments that initialize a
given @term{slot} is the union of the initialization arguments declared in
the @kwd{initarg} slot options in all the @term{slot specifiers}.
@item The @term{documentation string} for a @term{slot} is the value of
the @kwd{documentation} slot option in the most specific @term{slot}
specifier that contains one. If no @term{slot specifier} contains a
@kwd{documentation} slot option, the @term{slot} has no @term{documentation string}.
@end itemize
A consequence of the allocation rule is that a @term{shared slot} can be
@term{shadowed}. For example, if a class @mat{C@sub{1}} defines
a @term{slot} named @mat{S}
whose value for the @kwd{allocation} slot option is @kwd{class},
that @term{slot} is @term{accessible}
in @term{instances} of @mat{C@sub{1}} and all of its
@term{subclasses}. However, if @mat{C@sub{2}} is a @term{subclass}
of @mat{C@sub{1}} and also
defines a @term{slot} named @mat{S}@negthinspace{}, @mat{C@sub{1}}'s
@term{slot} is not shared
by @term{instances} of @mat{C@sub{2}} and its @term{subclasses}. When a class
@mat{C@sub{1}} defines a @term{shared slot}, any subclass @mat{C@sub{2}} of @mat{C@sub{1}} will share this single @term{slot}
unless the @symbolref{defclass, SYM} form for
@mat{C@sub{2}} specifies a @term{slot} of the same
@term{name} or there is a @term{superclass}
of @mat{C@sub{2}} that precedes @mat{C@sub{1}} in the @term{class precedence list} of
@mat{C@sub{2}} that defines a @term{slot} of the same name.
A consequence of the type rule is that the value of a @term{slot}