okflo
8/9/2015 9:18:00 AM
His Kennyness <kentilton@gmail.com> writes:
> Quick, before Costanza comes along to tell you how to succeed with
> your insanity, tell us what you are trying to achieve. Spoiler alert:
> we will then sort you out by helping you see what you are doing
> semantically, not by dicking with CLOS. And it will be vastly more
> helpful to you to achieve the former, because your new enlightenment
> will inform all the code you write.
Dear taurus, Pascal, Kaz, Ken, ...
thanks for your hints, perhaps/likely - as some of you mentioned - my
approach is wrong.
Let me give you an example, what I want: I have a class /vehicle/
which should be initialized by the user with the number of wheels. The
constructor makes out of the number-of-wheels a description.
(defclass vehicle ()
((description
:accessor description)
(number-of-wheels
:initarg :number-of-wheels
:accessor number-of-wheels)))
(defmethod initialize-instance :after ((v vehicle) &key)
(setf (description v)
(format nil "This is a Vehicle with ~A wheels."
(number-of-wheels v))))
Now a have a following class /caterpillar/ (in my real use case the
"succession" is much longer). which should be initialized by the user
with the number of caterpillar-tracks. Its constructor should
calculate the number of wheels (which won't be provided by the user in
that case), and then it should call the constructor of /vehicle/ and
generate the description.
(defclass caterpillar (vehicle)
((number-of-caterpillar-tracks
:initarg :number-of-caterpillar-tracks
:accessor number-of-caterpillar-tracks)))
(defmethod initialize-instance :after ((v caterpillar) &key)
(setf (number-of-wheels v)
(* 16 (number-of-caterpillar-tracks v))))
But that will not work - as I expected - because
"initialize-instance :after of /vehicle/" is called before
"initialize-instance :after of /caterpillar/", because of the reverse
class precedence order of :after methods, so number-of-wheels will be
first unbound.
So that lead to my initial question - a way to call the constructors
in the class precedence order, but after the primary
initialize-instance that does all the initarg/initform magic.
The only solution I see at the moment is putting the "make-instance"
calls into their own function (make-vehicle, make-caterpillar, ...),
and that calls primary methods (with call-next-method) and does the
initializion. But I would really like to have all these things hidden
behind make-instance...
hmmm, missing the wood for the trees? Thanks for any hints...
okflo