ruricolist / serapeum

Utilities beyond Alexandria
MIT License
420 stars 41 forks source link

Util suggestion: copy-object #100

Open Ambrevar opened 2 years ago

Ambrevar commented 2 years ago

I might be missing an existing implementation of copy-object, but I find it useful sometimes.

Shallow copy:

(defmethod copy-object (object &rest slot-overrides)
  (let ((class-sym (class-name (class-of object))))
    (apply #'make-instance class-sym
           (append
            slot-overrides
            (alexandria:mappend
             (lambda (slot)
               (list (intern (symbol-name slot) "KEYWORD")
                     (slot-value object slot)))
             (mopu:slot-names class-sym)))))) 

Thoughts?

ruricolist commented 2 years ago

I'll think about this. I don't want to take on the bigger issue of what it means to "copy" something (see Pitman's essay on the subject), but I could see a place for a shallow copy-object function that works for CLOS objects the same way copy-structure works for structs.