From: Joshua Taylor on
Is there an idiomatic way of implementing aggregated inherited values?
(If there's a canonical term for this, I'd be glad to learn it!) E.g.,
a hierarchy of features, where subclasses include (most) of the features
of their superclasses. Here's a technique that works; I'm just
wondering if there are more typical ways to do this. (Or perhaps this
is one of the kinds of things for whic method combinations are intended.)


(defclass featureset () ())

(defgeneric features (featureset)
(:method-combination append)
(:method :around ((f featureset))
(remove-duplicates (call-next-method)))
(:method append ((f featureset))
'()))

(defclass a (featureset) ())

(defmethod features append ((a a))
'(a1 a2 a3))

(defclass a-2 (a) ())

(defmethod features append ((a-2 a-2))
'(a4 a5))

(defclass nicer (featureset) ())

(defmethod features :around ((nicer nicer))
(substitute 'a5-nicer 'a5 (call-next-method)))

(defclass nicer-a-2 (a-2 nicer) ())

;; (features (make-instance 'nicer-a-2))
;; => (a4 a5-nicer a1 a2 a3)


Thanks for any suggestions, comments, &c., in advance!
//JT
From: Pascal Costanza on
On 08/05/2010 22:00, Joshua Taylor wrote:
> Is there an idiomatic way of implementing aggregated inherited values?
> (If there's a canonical term for this, I'd be glad to learn it!) E.g., a
> hierarchy of features, where subclasses include (most) of the features
> of their superclasses. Here's a technique that works; I'm just wondering
> if there are more typical ways to do this. (Or perhaps this is one of
> the kinds of things for whic method combinations are intended.)

Indeed, this is what method combinations are there for.


Pascal

>
>
> (defclass featureset () ())
>
> (defgeneric features (featureset)
> (:method-combination append)
> (:method :around ((f featureset))
> (remove-duplicates (call-next-method)))
> (:method append ((f featureset))
> '()))
>
> (defclass a (featureset) ())
>
> (defmethod features append ((a a))
> '(a1 a2 a3))
>
> (defclass a-2 (a) ())
>
> (defmethod features append ((a-2 a-2))
> '(a4 a5))
>
> (defclass nicer (featureset) ())
>
> (defmethod features :around ((nicer nicer))
> (substitute 'a5-nicer 'a5 (call-next-method)))
>
> (defclass nicer-a-2 (a-2 nicer) ())
>
> ;; (features (make-instance 'nicer-a-2))
> ;; => (a4 a5-nicer a1 a2 a3)
>
>
> Thanks for any suggestions, comments, &c., in advance!
> //JT


--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
 | 
Pages: 1
Prev: Google Code Jam 2010
Next: NETWORK MARKETING