koans clog defgeneric
so, there's advice qualifiers for DEFMETHOD and they have "standard combination" http://www.lispworks.com/documentation/HyperSpec/Body/07_ffb.htm :around and primary methods can call CALL-NEXT-METHOD can be used to invoke a less specific method for the class :before and :after are all always called as a onion with most specific on the outside
This commit is contained in:
parent
4cfca2c9c3
commit
6f09e0019c
|
@ -27,6 +27,8 @@
|
||||||
(defmethod value :after ((object access-counter))
|
(defmethod value :after ((object access-counter))
|
||||||
(incf (slot-value object 'access-count)))
|
(incf (slot-value object 'access-count)))
|
||||||
|
|
||||||
|
;; how is the signature for attributes decided?
|
||||||
|
;; do I just need to remember how (setf 'accessor') get arguments and those are all same?
|
||||||
(defmethod (setf value) :after (new-value (object access-counter))
|
(defmethod (setf value) :after (new-value (object access-counter))
|
||||||
(incf (slot-value object 'access-count)))
|
(incf (slot-value object 'access-count)))
|
||||||
;; wowy, so 'value has :accessor - both reader value and writer through (setf value)
|
;; wowy, so 'value has :accessor - both reader value and writer through (setf value)
|
||||||
|
@ -66,6 +68,8 @@
|
||||||
(defgeneric grab-lollipop-while-mom-is-nearby (was-nice-p)
|
(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 :around (was-nice-p) (if was-nice-p (call-next-method) :no-lollipop))
|
||||||
(:method (was-nice-p) (declare (ignore was-nice-p)) :lollipop))
|
(:method (was-nice-p) (declare (ignore was-nice-p)) :lollipop))
|
||||||
|
;; why are here two methods? I don't understand
|
||||||
|
;; what's the connection between DEFGENERIC and DEFMETHOD ?
|
||||||
|
|
||||||
(define-test lollipop
|
(define-test lollipop
|
||||||
(assert-equal :lollipop (grab-lollipop))
|
(assert-equal :lollipop (grab-lollipop))
|
||||||
|
@ -80,7 +84,10 @@
|
||||||
;; the previous time that it returned. If the countdown hits zero, :BANG
|
;; the previous time that it returned. If the countdown hits zero, :BANG
|
||||||
;; should be returned instead.
|
;; should be returned instead.
|
||||||
((remaining-time :reader remaining-time :initarg :time)))
|
((remaining-time :reader remaining-time :initarg :time)))
|
||||||
|
;; :reader remaining-time created DEFGENERIC over countdown object
|
||||||
|
;; so separately donig DEFMEHTOD could be as advice around, ok
|
||||||
|
|
||||||
|
;; is here OBJECT is a signifier? or a name for referencing? would OBJ do?
|
||||||
(defmethod remaining-time :around ((object countdown))
|
(defmethod remaining-time :around ((object countdown))
|
||||||
(let ((time (call-next-method)))
|
(let ((time (call-next-method)))
|
||||||
(if (< 0 time)
|
(if (< 0 time)
|
||||||
|
@ -92,11 +99,11 @@
|
||||||
(define-test countdown
|
(define-test countdown
|
||||||
(let ((countdown (make-instance 'countdown :time 4)))
|
(let ((countdown (make-instance 'countdown :time 4)))
|
||||||
(assert-equal 3 (remaining-time countdown))
|
(assert-equal 3 (remaining-time countdown))
|
||||||
(assert-equal ____ (remaining-time countdown))
|
(assert-equal 2 (remaining-time countdown))
|
||||||
(assert-equal ____ (remaining-time countdown))
|
(assert-equal 1 (remaining-time countdown))
|
||||||
(assert-equal ____ (remaining-time countdown))
|
(assert-equal 0 (remaining-time countdown))
|
||||||
(assert-equal ____ (remaining-time countdown))
|
(assert-equal :bang (remaining-time countdown))
|
||||||
(assert-equal ____ (remaining-time countdown))))
|
(assert-equal :bang (remaining-time countdown))))
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
|
@ -132,10 +139,24 @@
|
||||||
(define-test multiple-methods
|
(define-test multiple-methods
|
||||||
(let ((object (make-instance 'object)))
|
(let ((object (make-instance 'object)))
|
||||||
(frobnicate object)
|
(frobnicate object)
|
||||||
(assert-equal ____ (counter object)))
|
(assert-equal (+ 5000 70 300000 2000000) (counter object)))
|
||||||
(let ((object (make-instance 'bigger-object)))
|
(let ((object (make-instance 'bigger-object)))
|
||||||
(frobnicate object)
|
(frobnicate object) ; nope, don't understand, why 345678
|
||||||
(assert-equal ____ (counter object))))
|
(assert-equal (+ 8 70 600 5000 40000 300000 2000000 10000000) (counter object))))
|
||||||
|
;; attempting get read documentation on CALL-NEXT-METHOD
|
||||||
|
;; getting to http://www.lispworks.com/documentation/HyperSpec/Body/07_ffb.htm
|
||||||
|
;;; The semantics of standard method combination is as follows:
|
||||||
|
;; * If there are any around methods, the most specific around method is called. It supplies the value or values of the generic function.
|
||||||
|
;; * Inside the body of an around method, call-next-method can be used to call the next method. When the next method returns, the around method can execute more code, perhaps based on the returned value or values. The generic function no-next-method is invoked if call-next-method is used and there is no applicable method to call. The function next-method-p may be used to determine whether a next method exists.
|
||||||
|
;; * If an around method invokes call-next-method, the next most specific around method is called, if one is applicable. If there are no around methods or if call-next-method is called by the least specific around method, the other methods are called as follows:
|
||||||
|
;; -- All the before methods are called, in most-specific-first order. Their values are ignored. An error is signaled if call-next-method is used in a before method.
|
||||||
|
;; -- The most specific primary method is called. Inside the body of a primary method, call-next-method may be used to call the next most specific primary method. When that method returns, the previous primary method can execute more code, perhaps based on the returned value or values. The generic function no-next-method is invoked if call-next-method is used and there are no more applicable primary methods. The function next-method-p may be used to determine whether a next method exists. If call-next-method is not used, only the most specific primary method is called.
|
||||||
|
;; -- All the after methods are called in most-specific-last order. Their values are ignored. An error is signaled if call-next-method is used in an after method.
|
||||||
|
;; * If no around methods were invoked, the most specific primary method supplies the value or values returned by the generic function. The value or values returned by the invocation of call-next-method in the least specific around method are those returned by the most specific primary method.
|
||||||
|
;; In standard method combination, if there is an applicable method but no applicable primary method, an error is signaled.
|
||||||
|
;; The before methods are run in most-specific-first order while the after methods are run in least-specific-first order. The design rationale for this difference can be illustrated with an example. Suppose class C1 modifies the behavior of its superclass, C2, by adding before methods and after methods. Whether the behavior of the class C2 is defined directly by methods on C2 or is inherited from its superclasses does not affect the relative order of invocation of methods on instances of the class C1. Class C1's before method runs before all of class C2's methods. Class C1's after method runs after all of class C2's methods.
|
||||||
|
;; By contrast, all around methods run before any other methods run. Thus a less specific around method runs before a more specific primary method.
|
||||||
|
;; If only primary methods are used and if call-next-method is not used, only the most specific method is invoked; that is, more specific methods shadow more general ones.
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue