* 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
* Re: Edit a cl-struct by copy, or, slot-value for cl-structs
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:37 ` Michael Heerdegen
2 siblings, 1 reply; 9+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-10-29 18:43 UTC (permalink / raw)
To: help-gnu-emacs
Yuan Fu wrote:
> (make-xxx :slot1 (xxx-slot1 obj)
> :slot2 (xxx-slot2 obj)
> :slot3 newvalue)
How do you create the initial struct?
> Anyone knows how to define such a copy-with function without
> resorting to EIEIO?
"Enforce In-order Execution of I/O" ... and a joke (?)
<https://en.wikipedia.org/wiki/Enforce_In-order_Execution_of_I/O>
--
underground experts united
https://dataswamp.org/~incal
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Edit a cl-struct by copy, or, slot-value for cl-structs
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 19:15 ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-10-29 21:46 ` Yuan Fu
2021-10-29 21:37 ` Michael Heerdegen
2 siblings, 1 reply; 9+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2021-10-29 19:15 UTC (permalink / raw)
To: help-gnu-emacs
> (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!
The position of the slot descriptor in `cl--struct-class-slots` mirrors the
position of the slot in the actual objet, so you just need `aref/aset`.
It would also make a lot of sense to replace/extend the useless
`:copier` thingy of `cl-defstruct` with something like the
`:constructor`, so you could say
(cl-defstruct (my-foo
(:copier my-foo-with)
(:copier my-foo-with-a (b a)))
a b c)
and then use (my-foo-with foo :c 42) and (my-foo-with-a foo 153 "42")
Stefan
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Edit a cl-struct by copy, or, slot-value for cl-structs
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
0 siblings, 1 reply; 9+ messages in thread
From: Yuan Fu @ 2021-10-29 21:46 UTC (permalink / raw)
To: Stefan Monnier; +Cc: help-gnu-emacs
> On Oct 29, 2021, at 12:15 PM, Stefan Monnier via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
>
>> (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!
>
> The position of the slot descriptor in `cl--struct-class-slots` mirrors the
> position of the slot in the actual objet, so you just need `aref/aset`.
Ah, silly me!
>
> It would also make a lot of sense to replace/extend the useless
> `:copier` thingy of `cl-defstruct` with something like the
> `:constructor`, so you could say
>
> (cl-defstruct (my-foo
> (:copier my-foo-with)
> (:copier my-foo-with-a (b a)))
> a b c)
>
> and then use (my-foo-with foo :c 42) and (my-foo-with-a foo 153 "42”)
That’s cool. But my lazy fingers like better to write a single function and use it for everything ;-)
Yuan
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Edit a cl-struct by copy, or, slot-value for cl-structs
2021-10-29 21:46 ` Yuan Fu
@ 2021-10-31 18:52 ` Stefan Monnier
0 siblings, 0 replies; 9+ messages in thread
From: Stefan Monnier @ 2021-10-31 18:52 UTC (permalink / raw)
To: Yuan Fu; +Cc: help-gnu-emacs
>>> (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!
FWIW, I just pushed to `master` a patch which makes `slot-value` work to
read slots from structs. It doesn't help you, tho, because it still
doesn't let you set them, and in your case it really shouldn't since
they are defined as read-only.
>> and then use (my-foo-with foo :c 42) and (my-foo-with-a foo 153 "42”)
> That’s cool. But my lazy fingers like better to write a single function and
> use it for everything ;-)
But that tends to come at a performance cost.
Stefan
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Edit a cl-struct by copy, or, slot-value for cl-structs
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 19:15 ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2021-10-29 21:37 ` Michael Heerdegen
2021-10-29 22:02 ` Emanuel Berg via Users list for the GNU Emacs text editor
2 siblings, 1 reply; 9+ messages in thread
From: Michael Heerdegen @ 2021-10-29 21:37 UTC (permalink / raw)
To: help-gnu-emacs
Yuan Fu <casouri@gmail.com> writes:
> (make-xxx :slot1 (xxx-slot1 obj)
> :slot2 (xxx-slot2 obj)
> :slot3 newvalue)
Sorry for the dumb question, but isn't that more or less the same as
`xxx-copy' plus modifying slot3 of the copy?
Michael.
^ 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.