Is there a generic method for cloning CLOS objects?

前端 未结 4 1469
暗喜
暗喜 2020-12-10 16:37

I\'m looking for a way to clone CLOS objects in a shallow manner, so the created object would be of the same type with the same values in each slot, but a new instance. The

4条回答
  •  佛祖请我去吃肉
    2020-12-10 17:03

    This solution does not require sl-mob:

    (defun copy-slot (s d slot)
      `(setf (,slot ,d) (,slot ,s)))
    
    (defun copy-by-slots (s d slots)
      (assert (eql (class-of s) (class-of d)))
      (let ((f (lambda (s$) (eval (copy-slot s d s$)))))
        (mapcar f slots)))
    
    (copy-by-slots src dest quoted-list-of-slots)
    

提交回复
热议问题