guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: append! on a deep-cloned list slot


From: tomas
Subject: Re: append! on a deep-cloned list slot
Date: Mon, 29 Apr 2019 10:58:33 +0200
User-agent: Mutt/1.5.21 (2010-09-15)

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

[Prev in Thread] Current Thread [Next in Thread]