From: Okam via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: Michael Heerdegen <michael_heerdegen@web.de>,
Lars Ingebrigtsen <larsi@gnus.org>
Cc: 49809@debbugs.gnu.org, monnier@iro.umontreal.ca
Subject: bug#49809: [PATCH] Add macro 'pcase-setq'
Date: Thu, 12 Aug 2021 12:11:22 +0000 [thread overview]
Message-ID: <142fe5f4-663d-4236-a77c-0fcdfa528280@protonmail.com> (raw)
In-Reply-To: <87wnor5gxb.fsf@web.de>
[-- Attachment #1: Type: text/plain, Size: 1742 bytes --]
On 8/12/21 2:13 AM, Michael Heerdegen wrote:
> Lars Ingebrigtsen <larsi@gnus.org> writes:
>
>>> But Michael points out that it seems your code won't perform the
>>> assignment if the pattern doesn't match, which I find to be an
>>> odd behavior.
>
> I hope that this is even true in all cases.
>
>>> I'd expect a behavior like that of `pcase-let`, instead.
>>
>> ... because I have no opinion here, really -- behaving like `pcase-let'
>> would be good, but on the other hand, the current behaviour also kinda
>> sorta makes sense.
>
> Here is something else that is odd:
>
> (let ((a 17)
> (b 17)
> (x 17))
> (pcase-setq (or `((,a) [(,b)])
> x)
> '((1) [(2)]))
> (list a b x)) ;; ==> (1 2 nil)
>
> The first `or' branch matches, nevertheless is the binding of `x' being
> set to (a totally unrelated value) nil which doesn't make much sense.
>
>
> Michael.
>
I think that the behavior makes sense for a pattern-matching `let` and
maybe not so much for a destructuring `setq`, which was my intended use
case. I think that it is different because the effects of the
`pcase-setq` form could be less temporary than the effects of the
`pcase-let` form.
If thought of as a destructuring `setq`, then I see the value in
signaling an error when a pattern doesn't match, as you said, but I
guess that would be inconsistent with the value assignments that `or`
generates.
I've attached an example version that would set all of the named
variables to nil before assigning values from the pattern, but I feel
like it is not the best behavior. Please correct my understanding if
that is not what was meant by "a behavior like that of `pcase-let`".
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: pcase-setq-example.el --]
[-- Type: text/x-emacs-lisp; name=pcase-setq-example.el, Size: 1804 bytes --]
(require 'pcase)
(defmacro pcase-setq (pat val &rest args)
"Assign values to variables by destructuring with `pcase'.
Each PATTERN and VALUE is handled sequentially. PATTERN is a
normal `pcase' pattern. VALUE is an expression.
If a PATTERN does not match its VALUE, then the variables in
that PATTERN are set to nil.
\(fn PATTERN VALUE PATTERN VALUE ...)"
(declare (debug (&rest [pcase-PAT form])))
(cond
(args
(let ((arg-length (length args)))
(unless (= 0 (mod arg-length 2))
(signal 'wrong-number-of-arguments
(list 'pcase-setq (+ 2 arg-length)))))
(let ((result))
(while args
(push `(pcase-setq ,(pop args) ,(pop args))
result))
`(progn
(pcase-setq ,pat ,val)
,@(nreverse result))))
((pcase--trivial-upat-p pat)
`(setq ,pat ,val))
(t
(let* ((pcase-setq-unique-vars nil)
(expansion
(pcase-compile-patterns
val
(list (cons pat
(lambda (varvals &rest _)
(let ((flat-var-val-list))
(dolist (varval varvals)
(let ((var (car varval))
(val (cadr varval)))
(unless (memq var pcase-setq-unique-vars)
(push var pcase-setq-unique-vars))
(push var flat-var-val-list)
(push val flat-var-val-list)))
`(setq ,@(nreverse flat-var-val-list)))))))))
`(progn
(setq ,@(mapcan (lambda (x) (list x nil))
pcase-setq-unique-vars))
,expansion)))))
next prev parent reply other threads:[~2021-08-12 12:11 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
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 [this message]
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=142fe5f4-663d-4236-a77c-0fcdfa528280@protonmail.com \
--to=bug-gnu-emacs@gnu.org \
--cc=49809@debbugs.gnu.org \
--cc=larsi@gnus.org \
--cc=michael_heerdegen@web.de \
--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).