Let's say I have a class Awith several slots:
(defclass a ()
((a-1 :initarg :a-1)
(a-2 :initarg :a-2)))
And a class Bthat inherits from A:
(defclass b (a)
((b-1 :initarg :b-1)))
If I want to create an instance B, it will make-instanceoffer me slots :a-1, :a-2and :b-1.
Here's a crazy idea: what if I want to create an instance Busing an existing instance Aand fill only the slot b-1?
PS. Why this may be useful: if it Aimplements some common methods that it Binherits directly without adding anything new. In an alternative approach, when an instance Ashould be a slot in B, I would need to write trivial wrappers of methods to call these methods in this slot.
The only way I can think of: in an auxiliary constructor, decompose the object Aand pass the corresponding slots to make-instancefor B, that is:
(defun make-b (b-1 a-obj)
(with-slots (a-1 a-2) a-obj
(make-instance 'b :b-1 b-1 :a-1 a-1 :a-2 a-2)))
Are there any better ways to do this? (or perhaps this approach leads to a very poor design, and should I avoid it at all?)
source
share