From: Heiko Kirschke

Subject: Re: defclass slot with method-combination?

Date: 1998-9-4 3:53

Marc,

> I need to define an inheritance behavior that concatenates string-values > of superclass-initforms with local slot initforms. E.g. > > (defclass super() ((f :accessor f :initform "head")) (:metaclass > user-class)) > (defclass sub(super) ((f :accessor f :initform "tail")) (:metaclass > user-class)) > > I'd like to get the following: > > (f(make-instance'sub)) -> "head tail" > > > I didn't find a standard option in defclass slot-descriptions for this. > I'd like to define the concatenate combination for each meta-class > 'user-class'. > > Any idea? Thanks in advance!
try this: (defclass user-class (standard-class) ()) (defmethod clos:compute-effective-slot-definition ((the-class user-class) slot-name ;; The order of the direct slots in direct-slot-definitions may ;; be reversed in other LISPs (this is code written & tested with ;; ACL 4.3): direct-slot-definitions) (let ((slot-definition (call-next-method)) (new-initform nil)) (loop for slot in direct-slot-definitions as initform = (clos:slot-definition-initform slot) when (stringp initform) do ;; Collecting the result string could be done perhaps more ;; elegant: (setf new-initform (if new-initform (concatenate 'string initform " " new-initform) initform))) (when new-initform ;; Since at (call-next-method) both the initform and ;; initfunction of the effective-slot had been set, both must be ;; changed here, too: (setf (slot-value slot-definition 'clos::initform) new-initform) (setf (slot-value slot-definition 'clos::initfunction) (constantly new-initform))) slot-definition)) (defclass super () ((f :accessor f :initform "head")) (:metaclass user-class)) (defclass sub(super) ((f :accessor f :initform "tail")) (:metaclass user-class)) (f (make-instance 'sub)) ==> "head tail" -- Viele Gruesse, Heiko