Newsgroups: comp.lang.lisp
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!news2.near.net!howland.reston.ans.net!gatech!bloom-beacon.mit.edu!news.bu.edu!gw1.att.com!nntpa!nntpa.cb.att.com!lgm
From: lgm@polaris.ih.att.com (Lawrence G. Mayka)
Subject: Re: Copying CLOS instance?
In-Reply-To: buff@pravda.world's message of 17 Nov 1994 04:57:26 GMT
Message-ID: <LGM.94Nov20151510@polaris.ih.att.com>
Sender: news@nntpa.cb.att.com (Netnews Administration)
Nntp-Posting-Host: polaris.ih.att.com
Organization: AT&T Bell Laboratories, Naperville, Illinois, USA
References: <3998p8$es5@Times.Stanford.EDU> <KANDERSO.94Nov3131224@wheaton.bbn.com>
	<LGM.94Nov9082012@polaris.ih.att.com> <CzDnEv.4op@cogsci.ed.ac.uk>
	<BUFF.94Nov16235726@pravda.world>
Date: Sun, 20 Nov 1994 21:15:09 GMT
Lines: 40

In article <BUFF.94Nov16235726@pravda.world> buff@pravda.world (Richard Billington) writes:

   In article <CzDnEv.4op@cogsci.ed.ac.uk> jeff@aiai.ed.ac.uk (Jeff Dalton) writes:

      But I'd be happy to write my own copy fn if I could just get at all
      slots in a reasonable way.  I realize, of course, that people
      sometimes argue that what's a slot is application-specific (maybe
      some slots are stored off in hash tables someplace).  That kind
      of reasoning has been used against a number of useful things.

   Well, I'm a bit of an innocent with regards to the Metaobject
   Protocol, but I thought that's what it was all about. There's an MP
   function on standard-class type class called class-slots.
   (class-slots (find-class '<class>)) would give you a list of all the
   slots accessable in objects of class <class>.

This can certainly be done but the simple, naive way of doing it is
=slow=: construct a "blank" instance, compute the list of slot names,
iterate through it, repeatedly calling SLOT-VALUE and (SETF
SLOT-VALUE) with each (variable, and hence unoptimizable) slot name,
etc.  One could pick up some speed by calling the MOP's
STANDARD-INSTANCE-ACCESS, but one still has to compute the class's
total number of instance-allocated slots (to know how high to iterate
the index)--I don't think the MOP provides a single simple function to
do this.  One could also automatically generate a per-class
COPY-INSTANCE method when the class is defined, but this wastes space
with tens/hundreds/thousands of methods when a single method should be
quite sufficient for most purposes.

The point here is that this is the kind of case that C++ programmers
rightly complain about: The run-time system knows very well how to
perform a common operation very simply and efficiently; but it refuses
to do so and it doesn't let the programmer do so in any
reasonable/simple/portable way either.
--
        Lawrence G. Mayka
        AT&T Bell Laboratories
        lgm@ieain.att.com

Standard disclaimer.
