If I have much variables of an object, which I need to initialize, I have
to write something like this (or using slot-value):
(defmethod reset ((this map))
(with-slots (target-reached
robot-start-x robot-start-y
robot-x robot-y robot-speed-x robot-speed-y) this
(setf target-reached nil)
(setf robot-x robot-start-x)
(setf robot-y robot-start-y)
(setf robot-speed-x 0)
(setf robot-speed-y 0)))
Is there something like with-all-slots? I think I can write one, but I
wonder if there is a better way for such cases.
--
Frank Bu�, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
Frank Buss <··@frank-buss.de> writes:
> If I have much variables of an object, which I need to initialize, I have
> to write something like this (or using slot-value):
it's not with-all-slots, but reinitialize-instance may help:
(defmethod reset ((this map))
(reinitialize-instance this
:target-reached nil
:robot-x (robot-start-x this)
:robot-y (robot-stary-y this)
:robot-speed-x 0
:robot-speed-y 0))
reinitialize-instance does call shared-initialize, so if robot-x or
robot-y are set there then you can avoid setting them in this method.
p.s. - doesn't clos just kick ass?
--
-Marco
Ring the bells that still can ring.
Forget your perfect offering.
There is a crack in everything.
That's how the light gets in.
-Leonard Cohen
On Wed, 03 Nov 2004 18:19:09 +0100, Marco Baringer wrote:
> Frank Buss <··@frank-buss.de> writes:
>
>> If I have much variables of an object, which I need to initialize, I have
>> to write something like this (or using slot-value):
>
> it's not with-all-slots, but reinitialize-instance may help:
>
That's *incredibly* useful! Thanks for posting, I hadn't come across this
before.
>
> p.s. - doesn't clos just kick ass?
Yes, it really does.
Cheers,
Bill.
--
"If you give someone Fortran, he has Fortran. If you give someone Lisp,
he has any language he pleases." -- Guy Steele
"Marco Baringer" <··@bese.it> wrote:
> Frank Buss <··@frank-buss.de> writes:
>
>> If I have much variables of an object, which I need to initialize, I
>> have to write something like this (or using slot-value):
>
> it's not with-all-slots, but reinitialize-instance may help:
thanks, this works. But it is a bit dangerous, because you have to declare
accessors, otherwise the slot is not initialized and there is no warning
about this fact (at least in LispWorks).
--
Frank Bu�, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
Frank Buss <··@frank-buss.de> writes:
> thanks, this works. But it is a bit dangerous, because you have to
> declare accessors, otherwise the slot is not initialized and there
> is no warning about this fact (at least in LispWorks).
odd. slots should be left alone unless you have a shared-initialize
or reinitialize-instance method which changes them:
CL-USER> (defclass a () ((slot :initarg :slot :initform 'default-value)))
#<STANDARD-CLASS A>
CL-USER> (defparameter obj (make-instance 'a))
OBJ
CL-USER> (slot-value obj 'slot)
DEFAULT-VALUE
CL-USER> (setf (slot-value obj 'slot) 'something-else)
SOMETHING-ELSE
CL-USER> (reinitialize-instance obj)
#<A #x6906426>
CL-USER> (slot-value obj 'slot)
SOMETHING-ELSE
CL-USER> (reinitialize-instance obj :slot 'third-thing)
#<A #x6906426>
CL-USER> (slot-value obj 'slot)
THIRD-THING
CL-USER>
--
-Marco
Ring the bells that still can ring.
Forget your perfect offering.
There is a crack in everything.
That's how the light gets in.
-Leonard Cohen
"Marco Baringer" <··@bese.it> wrote:
> Frank Buss <··@frank-buss.de> writes:
>
>> thanks, this works. But it is a bit dangerous, because you have to
>> declare accessors, otherwise the slot is not initialized and there
>> is no warning about this fact (at least in LispWorks).
>
> odd. slots should be left alone unless you have a shared-initialize
> or reinitialize-instance method which changes them:
yes, that's right. I meant "initarg", not "accessor":
CL-USER > (defclass a () ((slot :initform 'default-value)))
#<STANDARD-CLASS A 214CF5AC>
CL-USER > (defparameter obj (make-instance 'a))
OBJ
CL-USER > (slot-value obj 'slot)
DEFAULT-VALUE
CL-USER > (setf (slot-value obj 'slot) 'something-else)
SOMETHING-ELSE
CL-USER > (reinitialize-instance obj)
#<A 214ECD64>
CL-USER > (slot-value obj 'slot)
SOMETHING-ELSE
CL-USER > (reinitialize-instance obj :slot 'third-thing)
#<A 214ECD64>
the hyperspec says, that only the initargs are initialized, if I
understand it correctly, so I think it is not an error, but a warning
would be nice, because the value was not changed:
CL-USER > (slot-value obj 'slot)
SOMETHING-ELSE
--
Frank Bu�, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
Frank Buss <··@frank-buss.de> writes:
> Is there something like with-all-slots? I think I can write one, but
> I wonder if there is a better way for such cases.
There is a conceptual problem here in that there is no reason to
assume that the set of slots at macro-expansion time is the same as
that at execution time.
My suggestion would be to write a macro-writing macro something like
(define-accessor-scope with-robot ()
speed location ...)
That defines a macro with-robot such that
(with-robot my-robot
(incf location speed))
works the obvious way.
--
Frode Vatvedt Fjeld