-
Notifications
You must be signed in to change notification settings - Fork 3
/
closette-tests.lisp
1945 lines (1627 loc) · 60.4 KB
/
closette-tests.lisp
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
;;;-*-Mode:LISP; Package: CLOSETTE; Base:10; Syntax:Common-lisp -*-
(in-package :closette)
;;; CLOSette tests
;;; From chapter 1
(defclass rectangle ()
((height :initform 0.0 :initarg :height)
(width :initform 0.0 :initarg :width)))
(defclass color-mixin ()
((cyan :initform 0 :initarg :cyan)
(magenta :initform 0 :initarg :magenta)
(yellow :initform 0 :initarg :yellow)))
(defclass color-rectangle (color-mixin rectangle)
((clearp :initform (y-or-n-p "But is it transparent?")
:initarg :clearp :accessor clearp)))
(defgeneric paint (x))
(defmethod paint ((x rectangle)) ;Method #1
(vertical-stroke (slot-value x 'height)
(slot-value x 'width)))
(defmethod paint :before ((x color-mixin)) ;Method #2
(set-brush-color (slot-value x 'cyan)
(slot-value x 'magenta)
(slot-value x 'yellow)))
(defmethod paint ((x color-rectangle)) ;Method #3
(unless (clearp x) (call-next-method)))
(setq door
(make-instance 'color-rectangle
:width 38 :height 84 :cyan 60 :yellow 55 :clearp nil))
(defun vertical-stroke (x y) (declare (ignore x y)) (values))
(defun set-brush-color (x y z) (declare (ignore x y z)) (values))
(paint door)
;;; test method combination
(defgeneric mctest (x))
(defmethod mctest :around ((x integer))
(format t "(:around integer)")
(call-next-method))
(defmethod mctest :around ((x number))
(format t "(:around number)")
(call-next-method))
(defmethod mctest :before ((x number))
(format t "(:before number)"))
(defmethod mctest ((x number))
(format t "(primary number)")
(1+ (call-next-method)))
(defmethod mctest :after ((x number))
(format t "(:after number)"))
(defmethod mctest :before ((x t))
(format t "(:before t)"))
(defmethod mctest ((x t))
(format t "(primary t)")
100)
(defmethod mctest :after ((x t))
(format t "(:after t)"))
(mctest 1)
#|(:around integer)(:around number)(:before number)(:before t)
(primary number)(primary t)(:after t)(:after number)
101|#
;;; following chapter 1
(pprint (macroexpand
'(defclass color-rectangle (color-mixin rectangle)
((clearp :initform (y-or-n-p "But is it transparent?")
:initarg :clearp :accessor clearp)))))
#|(ensure-class 'color-rectangle
:direct-superclasses
(list (find-class 'color-mixin) (find-class 'rectangle))
:direct-slots
(list
(list :name 'clearp :initform
'(y-or-n-p "But is it transparent?")
:initfunction
(function
(lambda nil (y-or-n-p "But is it transparent?")))
:initargs
'(:clearp)
:readers
'(clearp)
:writers
'((setf clearp)))))
|#
;;; original compute-slots
(defun original-compute-slots (class)
(mapcar #'(lambda (slot)
(make-effective-slot-definition
:name (slot-definition-name slot)
:initform (slot-definition-initform slot)
:initfunction (slot-definition-initfunction slot)
:initargs (slot-definition-initargs slot)))
(remove-duplicates
(mapappend #'class-direct-slots
(class-precedence-list class))
:key #'slot-definition-name
:from-end t)))
(equal (original-compute-slots (find-class 'color-rectangle))
(compute-slots (find-class 'color-rectangle)))
#|T|#
(pprint (macroexpand
'(defgeneric paint (x))))
#|(ensure-generic-function 'paint :lambda-list '(x))|#
(pprint (macroexpand
'(defmethod paint :before ((x color-mixin)) ; Method#2
(set-brush-color (slot-value x 'cyan)
(slot-value x 'magenta)
(slot-value x 'yellow)))))
#|(ensure-method (find-generic-function 'paint)
:lambda-list
'(x)
:qualifiers
'(:before)
:specializers
(list (find-class 'color-mixin))
:body
'(block paint
(set-brush-color (slot-value x 'cyan)
(slot-value x 'magenta)
(slot-value x 'yellow)))
:environment
(top-level-environment))
|#
(find-generic-function 'clearp)
#|#<Closette:Standard-Generic-Function CLOSETTE::CLEARP 16060700>|#
(clearp (make-instance 'color-rectangle :clearp t))
#|T|#
;;; change-class
(setq o1 (make-instance 'rectangle :height 10 :width 20))
(describe-object o1 *standard-output*)
#| A CLOS object
Printed representation: #<Rectangle 16166710>
Class: #<Standard-Class rectangle 15253764>
Structure
height <- 10
width <- 20
|#
(change-class o1 'color-mixin)
(describe-object o1 *standard-output*)
#| A CLOS object
Printed representation: #<Color-Mixin 16166710>
Class: #<Standard-Class color-mixin 15274440>
Structure
cyan <- 0
magenta <- 0
yellow <- 0
|#
(change-class o1 'standard-object)
(describe-object o1 *standard-output*)
#| A CLOS object
Printed representation: #<Standard-Object 16166710>
Class: #<Standard-Class standard-object 15071700>
Structure
|#
(sub-specializer-p (find-class 'color-mixin)
(find-class 'rectangle)
(find-class 'color-rectangle))
#|T|#
(sub-specializer-p (find-class 'rectangle)
(find-class 'rectangle)
(find-class 'color-rectangle))
#|NIL|#
;;; exercise
(defvar all-tables (make-hash-table :test #'eq))
(defun classes-to-applicable-methods-table (gf)
(let ((table (gethash gf all-tables nil)))
(unless table
(setq table (make-hash-table :test #'equal))
(setf (gethash gf all-tables) table))
table))
(defun better-apply-generic-function (gf args)
(let* ((required-classes
(mapcar #'class-of (required-portion gf args)))
(applicable-methods
(or (gethash required-classes
(classes-to-applicable-methods-table gf)
nil)
(setf (gethash required-classes
(classes-to-applicable-methods-table gf))
(compute-applicable-methods-using-classes
gf required-classes)))))
(if (null applicable-methods)
(error "No matching method for the~@
generic function ~S,~@
when called with arguments ~:S." gf args)
(apply-methods gf args applicable-methods))))
(better-apply-generic-function
(find-generic-function 'make-instance)
(list 'rectangle))
;;; From chapter 2:
(defun subclasses* (class)
(remove-duplicates
(cons class
(mapappend #'subclasses*
(class-direct-subclasses class)))))
(defun subclasses (class) (remove class (subclasses* class)))
(subclasses (find-class 'rectangle))
#|(#<Standard-Class COLOR-RECTANGLE>)|#
(defvar my-classes
(mapcar #'class-name
(subclasses (find-class 'standard-object))))
my-classes
#|(color-mixin rectangle
color-rectangle
standard-method
standard-generic-function
standard-class)
|#
(defun display-defclass (class-name)
(pprint (generate-defclass (find-class class-name)))
(values))
(defun generate-defclass (class)
`(defclass ,(class-name class)
,(mapcar #'class-name (class-direct-superclasses class))
,(mapcar #'generate-slot-specification (class-direct-slots class))))
(defun generate-slot-specification (slot)
`(,(slot-definition-name slot)
,@(when (slot-definition-initfunction slot)
`(:initform ,(slot-definition-initform slot)))
,@(when (slot-definition-initargs slot)
(mapappend #'(lambda (initarg) `(:initarg ,initarg))
(slot-definition-initargs slot)))
,@(unless (eq (slot-definition-allocation slot) ':instance)
`(:allocation ,(slot-definition-allocation slot)))
,@(when (slot-definition-readers slot)
(mapappend #'(lambda (reader) `(:reader ,reader))
(slot-definition-readers slot)))
,@(when (slot-definition-writers slot)
(mapappend #'(lambda (writer) `(:writer ,writer))
(slot-definition-writers slot)))))
(display-defclass 'rectangle)
#|(DEFCLASS RECTANGLE (STANDARD-OBJECT)
((HEIGTH :INITFORM 0.0 :INITARG :HEIGTH)
(WIDTH :INITFORM 0.0 :INITARG :WIDTH)))|#
(display-defclass 't)
#|(DEFCLASS T () ())|#
(display-defclass 'standard-object)
#|(DEFCLASS STANDARD-OBJECT (T) ()) |#
(defun display-defclass* (class-name)
(pprint (generate-defclass* (find-class class-name)))
(values))
(defun generate-defclass* (class)
`(defclass* ,(class-name class)
,(mapcar #'class-name (cdr (class-precedence-list class)))
,(mapcar #'(lambda (slot)
(generate-inherited-slot-specification class slot))
(class-slots class))))
(defun generate-inherited-slot-specification (class slot)
(let* ((source-class
(find-if #'(lambda (superclass)
(find (slot-definition-name slot)
(class-direct-slots superclass)
:key #'slot-definition-name))
(class-precedence-list class)))
(generated-slot-spec
(generate-slot-specification slot)))
(if (eq source-class class)
generated-slot-spec
(append generated-slot-spec
`(:inherited-from ,(class-name source-class))))))
(display-defclass* 'color-rectangle)
#|(defclass* color-rectangle
(color-mixin rectangle standard-object t)
((clearp :initform
(y-or-n-p "But is it transparent?")
:initarg
:clearp)
(cyan :initform
0
:initarg
:cyan
:inherited-from
color-mixin)
(magenta :initform
0
:initarg
:magenta
:inherited-from
color-mixin)
(yellow :initform
0
:initarg
:yellow
:inherited-from
color-mixin)
(height :initform
0.0
:initarg
:height
:inherited-from
rectangle)
(width :initform
0.0
:initarg
:width
:inherited-from
rectangle)))
|#
(defclass color-chart (rectangle color-mixin) ())
(mapcar #'class-name (class-precedence-list
(find-class 'color-rectangle)))
#|(COLOR-RECTANGLE COLOR-MIXIN RECTANGLE STANDARD-OBJECT T)|#
(mapcar #'class-name (class-precedence-list
(find-class 'color-chart)))
#|(COLOR-CHART RECTANGLE COLOR-MIXIN STANDARD-OBJECT T)|#
(defun in-order-p (c1 c2)
(flet ((in-order-at-subclass-p (sub)
(let ((cpl (class-precedence-list sub)))
(not (null (member c2 (cdr (member c1 cpl))))))))
(or (eq c1 c2)
(every #'in-order-at-subclass-p
(intersection (subclasses* c1)
(subclasses* c2))))))
(in-order-p (find-class 'color-mixin)
(find-class 'rectangle))
#|NIL|#
(in-order-p (find-class 'standard-object)
(find-class 't))
#|T|#
(defclass position ()
(x y))
(defclass cad-element (position) ())
(defclass display-element (position) ())
(defclass displayable-cad-element (display-element cad-element) ())
(defun has-diamond-p (class)
(some #'(lambda (pair)
(not (null (common-subclasses* (car pair)
(cadr pair)))))
(all-distinct-pairs (class-direct-subclasses class))))
(defun common-subclasses* (class-1 class-2)
(intersection (subclasses* class-1) (subclasses* class-2)))
(defun all-distinct-pairs (set)
(if (null set)
()
(append (mapcar #'(lambda (rest)
(list (car set) rest))
(cdr set))
(all-distinct-pairs (cdr set)))))
(has-diamond-p (find-class 'position))
#|t|#
(has-diamond-p (find-class 'rectangle))
#|nil|#
(defun generate-defgeneric (gf)
`(defgeneric ,(generic-function-name gf)
,(generic-function-lambda-list gf)))
(defun generate-defmethod (method &key show-body)
`(defmethod ,(generic-function-name (method-generic-function method))
,@(method-qualifiers method)
,(generate-specialized-arglist method)
,@(when show-body (list (method-body method)))))
(defun generate-specialized-arglist (method)
(let* ((specializers (method-specializers method))
(lambda-list (method-lambda-list method))
(number-required (length specializers)))
(append (mapcar #'(lambda (arg class)
(if (eq class (find-class 't))
arg
`(,arg ,(class-name class))))
(subseq lambda-list 0 number-required)
specializers)
(subseq lambda-list number-required))))
(defun display-generic-function (gf-name &key show-body)
(display-defgeneric gf-name)
(dolist (method (generic-function-methods (find-generic-function gf-name)))
(pprint (generate-defmethod method :show-body show-body)))
(values))
(defun display-defgeneric (gf-name)
(pprint (generate-defgeneric (find-generic-function gf-name)))
(values))
(display-generic-function 'paint :show-body t)
#|(DEFGENERIC PAINT (X))
(DEFMETHOD PAINT ((X RECTANGLE))
(BLOCK PAINT
(VERTICAL-STROKE (SLOT-VALUE X 'HEIGHT)
(SLOT-VALUE X 'WIDTH))))
(DEFMETHOD PAINT :BEFORE ((X COLOR-MIXIN))
(BLOCK PAINT
(SET-BRUSH-COLOR (SLOT-VALUE X 'CYAN)
(SLOT-VALUE X 'MAGENTA)
(SLOT-VALUE X 'YELLOW))))
(DEFMETHOD PAINT ((X COLOR-RECTANGLE))
(BLOCK PAINT
(UNLESS (CLEARP X) (CALL-NEXT-METHOD))))
|#
(display-generic-function 'clearp :show-body t)
#|(DEFGENERIC CLEARP (OBJECT))
(DEFMETHOD CLEARP ((OBJECT COLOR-RECTANGLE))
(SLOT-VALUE OBJECT 'CLEARP)) |#
(display-generic-function '(setf clearp) :show-body t)
#|(DEFGENERIC (SETF CLEARP) (NEW-VALUE OBJECT))
(DEFMETHOD (SETF CLEARP) ((OBJECT COLOR-RECTANGLE))
(SETF (SLOT-VALUE OBJECT 'CLEARP) NEW-VALUE)) |#
(display-generic-function 'shared-initialize)
#|(DEFGENERIC SHARED-INITIALIZE (INSTANCE SLOT-NAMES &KEY))
(DEFMETHOD SHARED-INITIALIZE ((INSTANCE STANDARD-OBJECT)
SLOT-NAMES &REST ALL-KEYS))|#
(defun all-generic-functions ()
(remove-duplicates
(mapappend #'class-direct-generic-functions
(subclasses* (find-class 't)))))
(defun class-direct-generic-functions (class)
(remove-duplicates
(mapcar #'method-generic-function
(class-direct-methods class))))
(mapcar #'generic-function-name (all-generic-functions))
#|(CLEARP PAINT UPDATE-INSTANCE-FOR-DIFFERENT-CLASS
REINITIALIZE-INSTANCE INITIALIZE-INSTANCE CHANGE-CLASS
MAKE-INSTANCE (SETF CLEARP) SHARED-INITIALIZE
PRINT-OBJECT \ldots)|#
(defun relevant-generic-functions (class ceiling)
(remove-duplicates
(mapcar #'method-generic-function
(mapappend #'class-direct-methods
(set-difference (class-precedence-list class)
(class-precedence-list ceiling))))))
(relevant-generic-functions (find-class 'color-rectangle)
(find-class 'standard-object))
#|(#<Standard-Generic-Function paint 16031414>
#<Standard-Generic-Function (setf clearp) 16021300>
#<Standard-Generic-Function clearp 16016120>)|#
(defun display-effective-method (gf args)
(let ((applicable-methods
(compute-applicable-methods-using-classes
gf (mapcar #'class-of (required-portion gf args)))))
(pprint
(if (null applicable-methods)
'(error "No applicable methods.")
(generate-effective-method gf applicable-methods)))))
(defun generate-effective-method (gf methods)
(declare (ignore gf))
(labels ((generate-method (method)
`(method ,@(cdr (generate-defmethod
method :show-body t))))
(generate-call-method (method next-methods)
`(call-method
,(generate-method method)
,(mapcar #'generate-method next-methods))))
(let ((primaries (remove-if-not #'primary-method-p methods))
(befores (remove-if-not #'before-method-p methods))
(afters (remove-if-not #'after-method-p methods)))
(if (null primaries)
'(error "No primary method")
`(progn
,@(mapcar
#'(lambda (method)
(generate-call-method method ()))
befores)
(multiple-value-prog1
,(generate-call-method (car primaries)
(cdr primaries))
,@(mapcar
#'(lambda (method)
(generate-call-method method ()))
(reverse afters))))))))
(display-effective-method (find-generic-function 'paint)
(list (make-instance 'color-rectangle
:clearp nil)))
#|(progn
(call-method
(method paint
:before
((x color-mixin))
(block paint
(set-brush-color (slot-value x 'cyan)
(slot-value x 'magenta)
(slot-value x 'yellow))))
nil)
(multiple-value-prog1
(call-method
(method paint
((x color-rectangle))
(block paint (unless (clearp x) (call-next-method))))
((method paint
((x rectangle))
(block paint
(vertical-stroke (slot-value x 'height)
(slot-value x 'width))))))))
|#
(display-effective-method (find-generic-function 'paint)
(list (make-instance 'rectangle)))
#|(progn
(multiple-value-prog1
(call-method
(method paint
((x rectangle))
(block paint
(vertical-stroke (slot-value x 'height)
(slot-value x 'width))))
nil)))
|#
(defun reader-method-p (method)
(let ((specializers (method-specializers method)))
(and (= (length specializers) 1)
(member (generic-function-name (method-generic-function method))
(mapappend #'slot-definition-readers
(class-direct-slots (car specializers)))
:test #'equal))))
(defun writer-method-p (method)
(let ((specializers (method-specializers method)))
(and (= (length specializers) 2)
(member (generic-function-name (method-generic-function method))
(mapappend #'slot-definition-writers
(class-direct-slots (cadr specializers)))
:test #'equal))))
;; renamed for duplicated definitions
(defun relevant-generic-functions-2 (class ceiling &key elide-accessors-p)
(remove-duplicates
(mapcar #'method-generic-function
(remove-if #'(lambda (m)
(and elide-accessors-p
(or (reader-method-p m)
(writer-method-p m))))
(mapappend #'class-direct-methods
(set-difference (class-precedence-list class)
(class-precedence-list ceiling)))))))
(relevant-generic-functions-2 (find-class 'color-rectangle)
(find-class 'standard-object)
:elide-accessors-p 't)
#|(#<Standard-Generic-Function paint 15316224>)|#
(defclass shape () ())
(defclass circle (shape) ())
(defclass triangle (shape) ())
(defclass pentagon (shape) ())
(defclass label-type () ())
(defclass top-labeled (label-type) ())
(defclass center-labeled (label-type) ())
(defclass bottom-labeled (label-type) ())
(defclass color () ())
(defclass fuschia (color) ())
(defclass orange (color) ())
(defclass magenta (color) ())
(defun make-programmatic-instance (superclass-names &rest initargs)
(apply #'make-instance
(find-programmatic-class
(mapcar #'find-class superclass-names))
initargs))
(defun find-programmatic-class (superclasses)
(let ((class (find-if
#'(lambda (class)
(equal superclasses
(class-direct-superclasses class)))
(class-direct-subclasses (car superclasses)))))
(if class
class
(make-programmatic-class superclasses))))
(defun make-programmatic-class (superclasses)
(make-instance 'standard-class
:name (mapcar #'class-name superclasses)
:direct-superclasses superclasses
:direct-slots ()))
(make-programmatic-instance '(circle orange top-labeled)
:title "Color Wheel"
:radius 10)
#|#<(Circle Orange Top-Labeled) 16023764>|#
(class-direct-subclasses (find-class 'circle))
#|(#<Standard-Class (circle orange top-labeled) 16021350>)|#
(setq i1 (make-programmatic-instance
'(circle orange top-labeled))
i2 (make-programmatic-instance
'(circle magenta bottom-labeled))
i3 (make-programmatic-instance
'(circle orange top-labeled)))
(class-direct-subclasses (find-class 'circle))
#|(#<Standard-Class (circle magenta bottom-labeled) 16043060>
#<Standard-Class (circle orange top-labeled) 16021350>)|#
;;; From chapter 3
(defclass counted-class (standard-class)
((counter :initform 0)))
(setf (find-class 'counted-rectangle)
(make-instance 'counted-class
:name 'counted-rectangle
:direct-superclasses (list (find-class 'rectangle))
:direct-slots ()))
(class-of (find-class 'rectangle))
#|#<Standard-Class STANDARD-CLASS 12505420>|#
(class-of (find-class 'counted-rectangle))
#|#<Standard-Class COUNTED-CLASS 69547893> |#
#|(slot-value (find-class 'rectangle) 'counter)
Error: The slot COUNTER is missing from the class
#<Standard-Class STANDARD-CLASS 15501664>.|#
(slot-value (find-class 'counted-rectangle) 'counter)
#|0|#
(defmethod make-instance :after ((class counted-class) &key)
(incf (slot-value class 'counter)))
(slot-value (find-class 'counted-rectangle) 'counter)
#|0|#
(make-instance 'counted-rectangle)
(slot-value (find-class 'counted-rectangle) 'counter)
#|1|#
(pprint (macroexpand
'(defclass counted-rectangle (rectangle)
()
(:metaclass counted-class))))
#|(ENSURE-CLASS 'COUNTED-RECTANGLE
:DIRECT-SUPERCLASSES
(LIST (FIND-CLASS 'RECTANGLE))
:DIRECT-SLOTS
(LIST)
:METACLASS
(FIND-CLASS 'COUNTED-CLASS))|#
(print-object (find-class 'counted-rectangle) *standard-output*)
#|#<Closette::Counted-Class CLOSETTE::COUNTED-RECTANGLE 16252370>|#
(print-object (find-class 'rectangle) *standard-output*)
#|#<Closette:Standard-Class CLOSETTE::RECTANGLE 15730444>|#
;;; alternative cpls
(defclass loops-class (standard-class) ())
(defclass flavors-class (standard-class) ())
(defmethod compute-class-precedence-list ((class loops-class))
(append (remove-duplicates
(depth-first-preorder-superclasses* class)
:from-end nil)
(list (find-class 'standard-object)
(find-class 't))))
(defmethod compute-class-precedence-list ((class flavors-class))
(append (remove-duplicates
(depth-first-preorder-superclasses* class)
:from-end t)
(list (find-class 'standard-object)
(find-class 't))))
(defun depth-first-preorder-superclasses* (class)
(if (eq class (find-class 'standard-object))
()
(cons class (mapappend #'depth-first-preorder-superclasses*
(class-direct-superclasses class)))))
(defclass a () ())
(defclass b () ())
(defclass c () ())
(defclass s (a b) ())
(defclass r (a c) ())
(defclass q-clos (s r) () (:metaclass standard-class))
(defclass q-loops (s r) () (:metaclass loops-class))
(defclass q-flavors (s r) () (:metaclass flavors-class))
(pprint (class-precedence-list (find-class 'q-flavors)))
#|(q-flavors s a b r c standard-object t)|#
(pprint (class-precedence-list (find-class 'q-loops)))
#|(q-loops s b r a c standard-object t)|#
(pprint (class-precedence-list (find-class 'q-clos)))
#|(q-clos s r a c b standard-object t)|#
(defclass vanilla-flavor () ())
(defmethod initialize-instance :around ((class flavors-class)
&rest all-keys
&key direct-superclasses)
(apply #'call-next-method
class
:direct-superclasses (or direct-superclasses
(list (find-class 'vanilla-flavor)))
all-keys))
(defclass flavors-test () () (:metaclass flavors-class))
(pprint (class-precedence-list (find-class 'flavors-test)))
#|(#<Flavors-Class flavors-test 16222110>
#<Standard-Class vanilla-object 16213600>
#<Standard-Class standard-object 15075604>
#<Standard-Class t 15060104>)|#
;;; attributes
(defclass attributes-class (standard-class) ())
(defun slot-definition-attributes (slot)
(getf slot ':attributes ()))
(defun (setf slot-definition-attributes) (new-value slot)
(setf (getf* slot ':attributes) new-value))
(defmethod compute-effective-slot-definition ((class attributes-class)
direct-slots)
(let ((normal-slot (call-next-method)))
(setf (slot-definition-attributes normal-slot)
(remove-duplicates
(mapappend #'slot-definition-attributes
direct-slots)))
normal-slot))
(defmethod compute-slots ((class attributes-class))
(let ((normal-slots (call-next-method)))
(flet ((initial-attribute-alist (slots)
(mapcar
#'(lambda (slot)
(cons (slot-definition-name slot)
(mapcar #'(lambda (attr) (cons attr nil))
(slot-definition-attributes slot))))
slots)))
(let ((alist (initial-attribute-alist normal-slots)))
(cons (make-effective-slot-definition
:name 'all-attributes
:initform alist
:initfunction #'(lambda () alist))
normal-slots)))))
(defun slot-attribute (instance slot-name attribute)
(cdr (slot-attribute-bucket instance slot-name attribute)))
(defun (setf slot-attribute) (new-value instance slot-name attribute)
(setf (cdr (slot-attribute-bucket
instance slot-name attribute))
new-value))
(defun slot-attribute-bucket (instance slot-name attribute)
(let* ((all-buckets (slot-value instance 'all-attributes))
(slot-bucket (assoc slot-name all-buckets)))
(unless slot-bucket
(error "The slot named ~A of ~S has no attributes."
slot-name instance))
(let ((attr-bucket (assoc attribute (cdr slot-bucket))))
(unless attr-bucket
(error "The slot named ~A of ~S has no attribute~@
named ~A." slot-name instance attribute))
attr-bucket)))
(defclass credit-rating ()
((level :attributes (date-set time-set)))
(:metaclass attributes-class))
(setq cr (make-instance 'credit-rating))
(slot-attribute cr 'level 'date-set)
#|NIL|#
(setf (slot-attribute cr 'level 'date-set) "12/15/90")
(slot-attribute cr 'level 'date-set)
#|"12/15/90"|#
(defclass monitored-credit-rating (credit-rating)
((level :attributes (last-checked interval)))
(:metaclass attributes-class))
(slot-value cr 'all-attributes)
#|((level . ((date-set . "12/15/90") (time-set . nil))))|#
(slot-value (make-instance 'monitored-credit-rating)
'all-attributes)
#| ((level . ((last-checked . nil) (interval . nil)
(date-set .nil ) (time-set .nil))))|#
;;; encapsulated classes
(defclass encapsulated-class (standard-class) ())
(defmethod initialize-instance :around ((class encapsulated-class)
&rest all-keys
&key direct-slots)
(let ((revised-direct-slots
(mapcar
#'(lambda (slot-properties)
(let ((pretty-name (getf slot-properties ':name))
(new-properties (copy-list slot-properties)))
(setf (getf* new-properties ':name) (gensym))
(setf (getf* new-properties ':pretty-name) pretty-name)
new-properties))
direct-slots)))
(apply #'call-next-method class
:direct-slots revised-direct-slots
all-keys)))
(defun slot-definition-pretty-name (slot)
(getf slot ':pretty-name))
(defun (setf slot-definition-pretty-name) (new-value slot)
(setf (getf* slot ':pretty-name) new-value))
(defun private-slot-value (instance slot-name class-name)
(slot-value instance (private-slot-name slot-name class-name)))
(defun private-slot-name (slot-name class-name)
(let* ((class (find-class class-name))
(slot (find slot-name
(class-direct-slots class)
:key #'slot-definition-pretty-name)))
(if slot
(slot-definition-name slot)
(error "The class ~S has no private slot named ~S."
class-name slot-name))))
(defclass c1 ()
((foo :reader foo :initform 100))
(:metaclass encapsulated-class))
(class-direct-slots (find-class 'c1))
(defclass c2 (c1)
((foo :reader foo :initform 200))
(:metaclass encapsulated-class))
(class-direct-slots (find-class 'c2))
(defgeneric mumble (o))
(defmethod mumble ((o c1))
(private-slot-value o 'foo 'c1))
(defmethod mumble ((o c2))
(+ (private-slot-value o 'foo 'c2)
(call-next-method)))
(mumble (make-instance 'c1))
#|100|#
(mumble (make-instance 'c2))
#|300|#
;;; default initargs
(pprint (macroexpand
'(defclass frame (rectangle)
()
(:metaclass default-initargs-class)
(:default-initargs :width 10))))
#|(ensure-class 'frame
:direct-superclasses
(list (find-class 'rectangle))
:direct-slots ()
:metaclass
(find-class 'default-initargs-class)
:direct-default-initargs
(list ':width 10))|#
(defclass default-initargs-class (standard-class)
((direct-default-initargs
:initarg :direct-default-initargs
:initform ()
:accessor class-direct-default-initargs)))
(defun compute-class-default-initargs (class)
(mapappend #'class-direct-default-initargs
(class-precedence-list class)))
(defmethod class-direct-default-initargs ((class standard-class))
())
(defmethod make-instance ((class default-initargs-class) &rest initargs)
(apply #'call-next-method
class
(append initargs
(compute-class-default-initargs class))))
(defclass frame (rectangle)
()
(:metaclass default-initargs-class)
(:default-initargs :width 10))
(setq f (make-instance 'frame :height 20))
(slot-value f 'height)
#|20|#
(slot-value f 'width)
#|10|#
(setq g (make-instance 'frame :height 20 :width 10))
(slot-value g 'height)
#|20|#
(slot-value g 'width)
#|10|#
;;; precomputed default initargs
(defclass default-initargs-class-2 (standard-class)
((direct-default-initargs
:initarg :direct-default-initargs
:initform ()
:accessor class-direct-default-initargs)
(effective-default-initargs
:accessor class-default-initargs)))
(defmethod finalize-inheritance :after ((class default-initargs-class-2))
(setf (class-default-initargs class)
(compute-class-default-initargs class)))
(defmethod make-instance ((class default-initargs-class-2) &rest initargs)
(apply #'call-next-method
class
(append initargs (class-default-initargs class))))
(defmethod class-default-initargs ((class standard-class))
())
(defclass frame-2 (rectangle)
()
(:metaclass default-initargs-class-2)
(:default-initargs :width 10))
(setq f (make-instance 'frame-2 :height 20))
(slot-value f 'height)
#|20|#
(slot-value f 'width)
#|10|#
;;;
(defmacro new-defclass (name direct-superclasses direct-slots
&rest options)
(let* ((metaclass-option