koans - clos and advice

objects similar to structures, but can have "generic" setters and creators
generic methods can have methods with qualifier :before :after :around
that in a way I don't fully understand registers them to be launched
with original function?
This commit is contained in:
efim
2022-08-06 09:00:44 +00:00
parent b7bccb9cfb
commit 4cfca2c9c3
2 changed files with 72 additions and 53 deletions

View File

@@ -29,21 +29,26 @@
(defmethod (setf value) :after (new-value (object access-counter))
(incf (slot-value object 'access-count)))
;; wowy, so 'value has :accessor - both reader value and writer through (setf value)
;; and these are gefgeneric, so I could after declaration of defgeneric add
;; more specific defmethod?
;; and they'd be specified to a specific type by having argument as (arg-name arg-class)
;; and 'qualifier' :after, that's trippy
(define-test defmethod-after
(let ((counter (make-instance 'access-counter :value 42)))
(assert-equal ____ (access-count counter))
(assert-equal ____ (value counter))
(assert-equal ____ (access-count counter))
(setf (value counter) 24)
(assert-equal ____ (access-count counter))
(assert-equal ____ (value counter))
(assert-equal ____ (access-count counter))
;; We read the value three more times and discard the result.
(value counter)
(value counter)
(value counter)
(assert-equal ____ (access-count counter))))
(let ((counter (make-instance 'access-counter :value 42)))
(assert-equal 0 (access-count counter))
(assert-equal 42 (value counter))
(assert-equal 1 (access-count counter))
(setf (value counter) 24)
(assert-equal 2 (access-count counter))
(assert-equal 24 (value counter))
(assert-equal 3 (access-count counter))
;; We read the value three more times and discard the result.
(value counter)
(value counter)
(value counter)
(assert-equal 6 (access-count counter))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -55,15 +60,17 @@
(defgeneric grab-lollipop ()
(:method () :lollipop))
;; wtf is :method here?
;; oh, it's shorthand of defmethod inside of defgeneric
(defgeneric grab-lollipop-while-mom-is-nearby (was-nice-p)
(:method :around (was-nice-p) (if was-nice-p (call-next-method) :no-lollipop))
(:method (was-nice-p) (declare (ignore was-nice-p)) :lollipop))
(define-test lollipop
(assert-equal ____ (grab-lollipop))
(assert-equal ____ (grab-lollipop-while-mom-is-nearby t))
(assert-equal ____ (grab-lollipop-while-mom-is-nearby nil)))
(assert-equal :lollipop (grab-lollipop))
(assert-equal :lollipop (grab-lollipop-while-mom-is-nearby t))
(assert-equal :no-lollipop (grab-lollipop-while-mom-is-nearby nil)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;