all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Edit a cl-struct by copy, or, slot-value for cl-structs
@ 2021-10-29 18:27 Yuan Fu
  2021-10-29 18:43 ` Emanuel Berg via Users list for the GNU Emacs text editor
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Yuan Fu @ 2021-10-29 18:27 UTC (permalink / raw)
  To: help-gnu-emacs

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


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2021-10-31 18:52 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-10-29 18:27 Edit a cl-struct by copy, or, slot-value for cl-structs Yuan Fu
2021-10-29 18:43 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-29 21:44   ` Yuan Fu
2021-10-29 19:15 ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-10-29 21:46   ` Yuan Fu
2021-10-31 18:52     ` Stefan Monnier
2021-10-29 21:37 ` Michael Heerdegen
2021-10-29 22:02   ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-29 23:14     ` Michael Heerdegen

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.