CL Question - how do I copy an object
Hi, this is a bit embarassing, but how do I *copy* an object? Guenther
Hi, this is a bit embarassing, but how do I *copy* an object? Guenther
On Nov 30, 2003, at 12:29 PM, Guenther Schmidt wrote: > Hi, > > this is a bit embarassing, but how do I *copy* an object? > > Guenther > Well, if you are trying to copy an atom, copy-symbol is probably what you want (with :copy-properties set to t). You can do a search from the LW help menu partial match on 'copy' and you will see different functions for copying sequences, lists, trees, etc. happy hacking, rca
On Sun, 30 Nov 2003 17:29:56 +0000, Guenther Schmidt <gue.schmidt@web.de> wrote: > this is a bit embarassing, but how do I *copy* an object? <http://www.nhplace.com/kent/PS/EQUAL.html>
* Guenther Schmidt wrote: > How do I *copy* a CLOS object? As others have pointed out this is in general an ill-defined notion (for instance: how deeply should you copy it? do you want sharing of structure to be preserved (which can be crucial)? Can it be copied at all (can a lock be copied?)?). Generally what you want to do is make a copy of an object *for some purpose* where the purpose is part of the application, and you need to define methods for that purpose. I think that, really, it's best to do this with special-purpose copiers, since using up some general name (like COPY or something) is really a bad idea since it implies that there's a well-defined notion of copying when there isn't. But if you want to use a generic name, I think the right approach would be something like this: (defgeneric copy-for (purpose object &key) ;; Methods on this should be EQL on PURPOSE, and specialise on ;; OBJECT. This GF should probably not exist... ) (defmethod copy-for ((purpose (eql 'shallow-standard-object)) (object standard-object) &key) ;; really anything with a reasonably standard MOP, modulo packages. ;; This is probably far from optimal #+LispWorks (loop with c = (class-of object) with new = (allocate-instance c) for s in (clos:class-slots c) for sn = (clos:slot-definition-name s) when (slot-boundp object sn) do (setf (slot-value new sn) (slot-value object sn)) finally (return new))) --tim
Guenther Schmidt writes: > Dear Nils, > > well the idea was to take a "snapshot" of an objects state, well the > state of the slots, so at some later point in time it would be possible > to "reset" that object. Well, this is sufficiently frequent that the C++ crowd calls this "The Memento Pattern". But that's a fair cry from the original "how do I copy an object" question. In fact, the Memento may hold only a small subset of the object's state, depending on the application. So, as others have said, canned solutions won't work: you'll have to roll your own on a class-by-class basis.