[comp.lang.lisp] Problem with CLOS

behrens@boulder (John Behrens) (07/02/90)

I spent the best part of a day chasing what turned out
to be the following:

(defmethod foo ((state state-class))
  (with-slots (bar) state   ;bar is a list
    (dotimes (i (length bar))
      (setf (nth i bar) <some value>))))

The behavior I got was that bar in *all* instances of state
was changed not just the instance passed into the method.

The following works:

(defmethod foo ((state state-class))
  (with-slots (bar) state   ;bar is a list
    (let ((baz
	    (make-sequence 'list (length bar) :initial-element 0)))
      (dotimes (i (length bar)(setf bar baz))
	(setf (nth i baz) <some value>)))))

I'm working in common LISP with CLOS on a MicroExplorer.
Does anyone have any idea what the problem is?

Jon

miller@cam.nist.gov (Bruce R. Miller) (07/04/90)

In article <22986systhvu@boulder.Colorado.EDU>, John Behrens writes: 
>I spent the best part of a day chasing what turned out
>to be the following:
>
>(defmethod foo ((state state-class))
>  (with-slots (bar) state   ;bar is a list
>    (dotimes (i (length bar))
>      (setf (nth i bar) <some value>))))
>
>The behavior I got was that bar in *all* instances of state
>was changed not just the instance passed into the method.
>  ...
>Does anyone have any idea what the problem is?
>
>Jon

Presumably the slot BAR is not a CLASS slot ...

By any chance are you initializing the slot BAR in STATE with a 
Constant (eg quoted) list?  Or in any other fashion such that
each slot points to the SAME (eq) list, as opposed to copies or
otherwise unique per instance lists.

If so, then after FOO, all instances are still pointing to the same
list, which by now has new stuff in it; hence the appearance that
all instances are being modified.

If not... never mind ...

bruce