Suppose you make an instance of the class foo:
(defclass foo ()
((test :accessor test :initform #'equal :initarg :test)))
(setf foo (make-instance 'foo :test #'eql))
Its test
slot will be set to #'eql:
(test foo) => #'eql
If you want to make a structural clone (for lack of a better term)
of *foo*
, you might try:
(setf new-foo (make-instance (type-of foo)))
But *new-foo*
's test slot won't be set properly:
(test new-foo) => #'equal
For simple classes, this is no problem but suppose we have
a graph from CL-Graph and want to make a copy of that:
(make-graph (type-of old-graph)
:vertex-test (vertex-test old-graph)
:vertex-key (vertex-key old-graph)
:edge-test (edge-test old-graph)
:edge-key (edge-key old-graph)
:default-edge-type (default-edge-type old-graph)
:default-edge-class (default-edge-class old-graph)
:directed-edge-class (directed-edge-class old-graph)
:undirected-edge-class (undirected-edge-class old-graph))))
Yuck!
Copy-template is a reasonable, though not perfect, solution to this
problem; it creates a structural copy of an object such that the copy
has all of its initargs correctly set.
[CL-Graph]: http://common-lisp.net/projects/cl-graph/
copy-template < standard-object > |