help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Edit a cl-struct by copy, or, slot-value for cl-structs


From: Yuan Fu
Subject: Edit a cl-struct by copy, or, slot-value for cl-structs
Date: Fri, 29 Oct 2021 11:27:51 -0700

I’m trying to make the cl-structs that I define immutable, and only edit them 
by copying. One way to do it is just by convention: I promise myself that I 
never setf a struct without making a copy. A better way is to use :read-only 
attribute for the slot to enforce it. But then there is not convenient way to 
edit by copy: I need to manually construct a new struct, copy slot values from 
the old one, like so:

(make-xxx :slot1 (xxx-slot1 obj)
          :slot2 (xxx-slot2 obj)
          :slot3 newvalue)

It gets tiring very quickly, I’m hoping for something like

(copy-with obj :slot3 newvalue)

Anyone knows how to define such a copy-with function without resorting to 
EIEIO? I can get the slots of a struct by 

(mapcar #'cl--slot-descriptor-name
        (cl--struct-class-slots (get (type-of obj) 'cl--class)))

But I can’t get any further as I can’t find any generic getter and setter for 
cl-struct’s. Something like slot-value. So close!

Thanks,
Yuan


reply via email to

[Prev in Thread] Current Thread [Next in Thread]