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 14:57:51 +0200
User-agent: Mutt/1.5.21 (2010-09-15)

On Mon, Apr 29, 2019 at 11:12:07AM +0100, Brandon Invergo wrote:
> Hi,
> 
> Thanks for your response.
> 
> address@hidden writes:
> 
> > 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.
> 
> My reading of this was that a new instance is allocated and, since the
> default #:allocation slot option is '#:instance', GOOPS "creates
> separate storage for this slot in each new instance of the containing
> class (and its subclasses)."  So I assumed that new storage is allocated
> for the slot and the data is copied over, which meant that the whole
> list was copied.

That's it: the slot doesn't "contain" the list, just the list reference.
Since the list isn't an object (in the GOOPS sense), deep-clone doesn't
know how to descend there. It just copies the thing in the slot, which
is a list reference ("the list" doesn't exist actually "down there",
but just in your view).

> I guess the key words "by reference" carried more weight than I mentally
> allocated to them.  GOOPS created separate storage for a new pointer,
> not a whole list, and the only the address of the list was copied into
> this pointer.  Tricky, but it makes sense I guess.

Exactly.

> In my case, I can simply get rid of the deep-clones and instead create a
> fresh instance and manually copy some of the slots from the old
> instance.

I think you can teach deep-copy to do the right thing: if your GOOPS
object "knows" that this slot "is" a list, you can tell GOOPS via
"define-method" how deep cloning is supposed to work for this object
(I'm not very deep in GOOPS myself, perhaps someone can chime-in
with a more knowledgeable approach).

Note that the behaviour actually makes sense: when the thing hanging
off your reference is immutable (think e.g. big numbers), you don't
want to copy the whole kaboodle. Likewise if you are *treating* it as
immutable.

> Laziness and programmers are dangerous bedfellows.

Laziness is one of the virtues of a good programmer :-)

Cheers
-- tomás

Attachment: signature.asc
Description: Digital signature


reply via email to

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