all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [PATCH] gv.el: minor change, cleaning up
@ 2021-06-16  5:06 Hu Lucius
  2021-06-16 13:03 ` Stefan Monnier
  0 siblings, 1 reply; 2+ messages in thread
From: Hu Lucius @ 2021-06-16  5:06 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1692 bytes --]

When defining `gv-expander` of `alist-get`, the following binding
specification seems to have a redundant `setq` form.

 ((set-exp
   `(if ,p (setcdr ,p ,v)
      ,(funcall setter
                `(cons (setq ,p (cons ,k ,v))
                       ,getter)))))

Here `set-exp` expands to an s-expression that updates the value
of an alist. `p` is a boolean that equals `(assq KEY ALIST)`
(or any other test function specified in the `alist-get` form).
`k` and `v` are KEY and VALUE respectively.

Evaluate `(macroexpand-all `(setf (alist-get key alist) value))` and
it returns the following:

  (let* () ;; /* bindings omitted */
    (progn
     (if p
         (setcdr p v)
       (setq alist
             (cons (setq p
                         (cons key v))
                   alist)))
     v))

It seems to me that `(setq p ..)` is unnecessary, i.e. we can simply
use

 (setq alist (cons (cons key v) alist)

This commit fixes that.

Signed-off-by: Lucius Hu <lebensterben@users.noreply.github.com>
---
 lisp/emacs-lisp/gv.el | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lisp/emacs-lisp/gv.el b/lisp/emacs-lisp/gv.el
index f08f7ac115..cf6cae4fd8 100644
--- a/lisp/emacs-lisp/gv.el
+++ b/lisp/emacs-lisp/gv.el
@@ -407,7 +407,7 @@ alist-get
                        (let ((set-exp
                               `(if ,p (setcdr ,p ,v)
                                  ,(funcall setter
-                                           `(cons (setq ,p (cons ,k ,v))
+                                           `(cons (cons ,k ,v)
                                                   ,getter)))))
                          `(progn
                             ,(cond
-- 
2.32.0

[-- Attachment #2: Type: text/html, Size: 2168 bytes --]

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

* Re: [PATCH] gv.el: minor change, cleaning up
  2021-06-16  5:06 [PATCH] gv.el: minor change, cleaning up Hu Lucius
@ 2021-06-16 13:03 ` Stefan Monnier
  0 siblings, 0 replies; 2+ messages in thread
From: Stefan Monnier @ 2021-06-16 13:03 UTC (permalink / raw)
  To: Hu Lucius; +Cc: emacs-devel

> Evaluate `(macroexpand-all `(setf (alist-get key alist) value))` and
> it returns the following:
>
>   (let* () ;; /* bindings omitted */
>     (progn
>      (if p
>          (setcdr p v)
>        (setq alist
>              (cons (setq p
>                          (cons key v))
>                    alist)))
>      v))
>
> It seems to me that `(setq p ..)` is unnecessary,

It's unnecessary for `setf` (and `push`, and `cl-pushnew`, and
`cl-rotatef`, and `cl-callf` ;-) but it can be necessary for more
complex settings.  I can't remember what was the setting where I found
that I needed it, sadly.  Clearly it must be one where we use the getter
(or the setter) after having used the setter.


        Stefan




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

end of thread, other threads:[~2021-06-16 13:03 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-06-16  5:06 [PATCH] gv.el: minor change, cleaning up Hu Lucius
2021-06-16 13:03 ` Stefan Monnier

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.