unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: Okam <okamsn@protonmail.com>
Cc: 49809@debbugs.gnu.org
Subject: bug#49809: [PATCH] Add macro 'pcase-setq'
Date: Wed, 04 Aug 2021 19:06:11 -0400	[thread overview]
Message-ID: <jwvwnp0su3m.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <07ad5892-3343-4c35-00e3-9753bca5f67b@protonmail.com> (Okam's message of "Sun, 01 Aug 2021 17:20:41 +0000")

> This patch adds a `setq`-like equivalent to `pcase-let`.
> This is convenient when one wants the bindings to exist outside of
> a `let` form.

Thanks.

> This macro expands into multiple `setq` calls that are combined where
> possible.

I don't think we should try and combine them: it's not worth the
code complexity.  Personally I'd even restrict the calling convention to
(pcase-setq PAT VAL), but if you want to accept the more general case
with multiple PAT+VAL, I'd prefer expanding it to a (progn (pcase-setq
PAT1 VAL1) ...).  I think the resulting code would be simpler/cleaner.

> Please let me know what should be changed.

See a few more comments below.

> Subject: [PATCH] Add macro 'pcase-setq'
>
> * doc/lispref/control.texi: Document this macro.
> * lisp/emacs-lisp/pcase.el: Add this macro.

Please include the "section/function" info,
e.g. I'd write the message as:

    * lisp/emacs-lisp/pcase.el (pcase-setq): New macro.

    This macro is the 'setq' equivalent of 'pcase-let'.

    * doc/lispref/control.texi (Destructuring with pcase Patterns): Document it.

> +@defmac pcase-setq pattern value@dots{}
> +Bind variables to values in a @code{setq} form, destructuring each
> +@var{value} according to its respective @var{pattern}.
> +@end defmac

I prefer keeping "bind" for the case where we create new variables
(i.e. let-bindings) rather than for assignments.

> +;;;###autoload
> +(defmacro pcase-setq (&rest args)
> +  "Bind values by destructuring with `pcase'.

Same here.

> +\(fn PATTERN VALUE PATTERN VALUE ...)"
> +  (declare (debug (&rest [pcase-PAT form])))
> +  (let ((results)
> +        (pattern)
> +        (value))
> +    (while args
> +      (setq pattern (pop args)
> +            value (pop args))
> +      (push (if (pcase--trivial-upat-p pattern)
> +                (list 'setq pattern value)
> +              (pcase-compile-patterns
> +               value
> +               (list (cons pattern
> +                           (lambda (varvals &rest _)
> +                             (cons 'setq
> +                                   (mapcan (lambda (varval)
> +                                             (let ((var (car varval))
> +                                                   (val (cadr varval)))
> +                                               (list var val)))
> +                                           varvals)))))))
> +            results))

Looks good.  But could you add a few corresponding tests to
`test/lisp/emacs-lisp/pcase-tests.el`, including tests for things like

    (pcase-setq `(,a ,b) nil)


-- Stefan






  parent reply	other threads:[~2021-08-04 23:06 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-01 17:20 bug#49809: [PATCH] Add macro 'pcase-setq' Okam via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-08-04  7:14 ` Lars Ingebrigtsen
2021-08-04 23:06 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2021-08-05  1:02   ` Michael Heerdegen
2021-08-05 13:34     ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-08-05 15:00       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-08-06  1:42       ` Michael Heerdegen
2021-08-06  4:07         ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-08-06  4:28           ` Michael Heerdegen
2021-08-06 22:33   ` Okam via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-08-07  2:11     ` Michael Heerdegen
2021-08-11 21:57       ` Lars Ingebrigtsen
2021-08-12  6:13         ` Michael Heerdegen
2021-08-12 12:11           ` Okam via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-08-12 15:06           ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-08-13  2:55             ` Michael Heerdegen
2021-08-13  5:17               ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-08-13  5:26                 ` Michael Heerdegen
2021-08-07 15:42     ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-08-09  0:28       ` Michael Heerdegen
2021-08-09 12:51         ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-08-10  3:13           ` Michael Heerdegen
2021-08-12 16:13             ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors

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=jwvwnp0su3m.fsf-monnier+emacs@gnu.org \
    --to=bug-gnu-emacs@gnu.org \
    --cc=49809@debbugs.gnu.org \
    --cc=monnier@iro.umontreal.ca \
    --cc=okamsn@protonmail.com \
    /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.
Code repositories for project(s) associated with this public inbox

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

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).