On Mon, Apr 29, 2019 at 09:44:09AM +0100, Brandon Invergo wrote:
> Hello,
> 
> I don't understand the following behavior:
> 
> scheme@(guile-user)> (use-modules (oop goops))
> scheme@(guile-user)> (define-class <xyzzy> () (gob #:accessor gob 
> #:init-keyword #:gob))
> scheme@(guile-user)> (define a (make <xyzzy> #:gob '(foo)))
> scheme@(guile-user)> (define b (deep-clone a))
> scheme@(guile-user)> (append! (gob b) '(bar))
> $1 = (foo bar)
> scheme@(guile-user)> (gob a)
> $2 = (foo bar)
> 
> Why does append! also modify the list in object a?  I was under the
> impression that deep-clone allocates new memory for all slots in b.

Quoth the docs

" -- generic: deep-clone
  -- method: deep-clone (self <object>)
     Return a “deep” clone of SELF.  The default method makes a deep
     clone by allocating a new instance and copying or cloning slot
     values from self to the new instance.  If a slot value is an
     instance (satisfies ‘instance?’), it is cloned by calling
     ‘deep-clone’ on that value.  Other slot values are copied either as
     immediate values or by reference."

Now a list isn't an object instance (i.e. it says #f to instance?).
You'll either have to override your clone method to be more savvy
or do something similar.

Probably you'd want to watch out whenever your objects have mutable
stuff in their slots which isn't an object...

Cheers
-- t

Attachment: signature.asc
Description: Digital signature

Reply via email to