Lisp HUG Maillist Archive

copy-object

Given a simple CLOS object with just a few slots, I
want to make a copy of it, such that each slot of the
copy is EQ the corresponding slot of the original.  Is
there a copy-object function or equivalent?


__________________________________________________
Do You Yahoo!?
Yahoo! Sports - Coverage of the 2002 Olympic Games
http://sports.yahoo.com


Re: copy-object

Eric Smith <eric3057@yahoo.com> writes:

> Given a simple CLOS object with just a few slots, I want to make a
> copy of it, such that each slot of the copy is EQ the corresponding
> slot of the original.  Is there a copy-object function or
> equivalent?

You might want to read this article:

  <http://world.std.com/~pitman/PS/EQUAL.html>

Edi.

-- 

Dr. Edmund Weitz
Hamburg
Germany

The Common Lisp Cookbook
<http://cl-cookbook.sourceforge.net/>


Re: copy-object

* Eric Smith wrote:
> That article is about the ambiguity of words such as
> copy, equal, etc.  The copy operation I want is
> basically the same operation as copy-structure, but
> applied to objects instead of structures.  Since
> copy-structure is in the hyperspec, it doesn't make
> sense to me to omit copy-object just because of the
> concerns in that article.  How do those concerns apply
> to objects differently than to structures?  Here is
> what happens when I try using copy-structure to copy
> an object:

There are really two points. One is that this kind of trivial shallow
copy is rather seldom useful: instead you typically need to do
something with the slots which respects the semantics of copying you
want to get.  Without the MOP you can do this using some some protocol
like this:

(defun semantically-copy-object (o)		;really bad name
  (let ((new (allocate-instance (class-of o))))
    (semantically-copy-slots o new)
    new))

(defgeneric semantically-copy-slots (o copy)	;likewise
  ;; users define methods on this.
  (:method-combination progn))

(defmethod semantically-copy-slots progn (o new)
  (declare (ignore o new)))

The second is that you can't do this for structures - you can't get a
new blank structure from an old one because MAKE-INSTANCE doesn't work
there.  So you need to get a toehold somehow, and COPY-STRUCTURE gives
you that toehold.  It would (I think) be better to have provided
ALLOCATE-STRUCTURE which would have created a `blank' structure
instance.  But then again, before there were methods it is not quite
so easy to implement the protocol above.

I had a paper which was even more explicitly about this than KMP's at
some Lisp conference, although likely not so well written.

--tim



	    


Updated at: 2020-12-10 09:02 UTC