Let's say I have class A
with a couple of slots:
(defclass a ()
((a-1 :initarg :a-1)
(a-2 :initarg :a-2)))
And class B
that inherits from A
:
(defclass b (a)
((b-1 :initarg :b-1)))
If I want to instantiate B
, make-instance
will offer me slots :a-1
, :a-2
and :b-1
.
Here is a crazy idea: what if I want to instantiate B
using the existing instance of A
and only filling slot b-1
?
PS. Why it can be useful: if A
implements some generic methods that B
inherits directly, without adding anything new. In alternative approach, making instance of A
to be a slot in B
, I would need to write trivial method wrappers to invoke these methods on that slot.
The only way I can think of: in auxiliary constructor decompose object A
and pass corresponding slots to make-instance
for B
, i.e.:
(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 better ways of doing this? (or perhaps, this approach leads to very bad design and I should avoid it altogether?)