Is there a generic method for cloning CLOS objects?

前端 未结 4 1471
暗喜
暗喜 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

    I mention a dirty trick producing a clone of a CLOS instance.

    (defclass cl () ((sl1 :initarg :sl1) (sl2 :initarg :sl2)))
    
    (defmethod update-instance-for-different-class ((copy cl) (original cl) &key)
      (setf clone copy))
    
    (setf a (make-instance 'cl :sl1 111 :sl2 222))
    
    (change-class a 'cl)
    
    (eq clone a) -> NIL
    (eql (slot-value a 'sl1) (slot-value clone 'sl1)) -> T
    

    Implies CLOS itself needs a notion of clone.

提交回复
热议问题