From: Yuan Fu <casouri@gmail.com>
To: help-gnu-emacs <help-gnu-emacs@gnu.org>
Subject: Edit a cl-struct by copy, or, slot-value for cl-structs
Date: Fri, 29 Oct 2021 11:27:51 -0700 [thread overview]
Message-ID: <06C2AE7C-C3E9-4E34-9394-B431173639E9@gmail.com> (raw)
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
next reply other threads:[~2021-10-29 18:27 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-10-29 18:27 Yuan Fu [this message]
2021-10-29 18:43 ` Edit a cl-struct by copy, or, slot-value for cl-structs 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=06C2AE7C-C3E9-4E34-9394-B431173639E9@gmail.com \
--to=casouri@gmail.com \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).