GNU bug report logs - #49809
[PATCH] Add macro 'pcase-setq'

Previous Next

Package: emacs;

Reported by: Okam <okamsn <at> protonmail.com>

Date: Sun, 1 Aug 2021 17:21:01 UTC

Severity: normal

Tags: patch

Fixed in version 28.1

Done: Lars Ingebrigtsen <larsi <at> gnus.org>

Bug is archived. No further changes may be made.

Full log


View this message in rfc822 format

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Michael Heerdegen <michael_heerdegen <at> web.de>
Cc: okamsn <at> protonmail.com, 49809 <at> debbugs.gnu.org, monnier <at> iro.umontreal.ca
Subject: bug#49809: [PATCH] Add macro 'pcase-setq'
Date: Wed, 11 Aug 2021 23:57:29 +0200
Michael Heerdegen <michael_heerdegen <at> web.de> writes:

>> +(defmacro pcase-setq (pat val &rest args)
>> +  "Assign values to variables by destructuring with `pcase'.
>> +
>> +\(fn PATTERN VALUE PATTERN VALUE ...)"

It seems like there was general agreement to adding this, so I've now
applied Earl's patch, with the following addition to the doc string:

> Can we maybe enhance the docstring a bit?  I think we should at least
> cover these points:
>
> - The PATTERNs are normal `pcase' patterns, the VALUES are expressions.
>
> - Evaluation happens sequentially as in `setq' (not in parallel)
>
> - When a PATTERN doesn't match it's VALUE, the pair is silently skipped
>   (completely, no partial assignments are performed, AFAIU)
>
> Maybe adding a simple example would not be too bad as well?

Stefan Monnier <monnier <at> iro.umontreal.ca> writes:

> The result is the same whether `pcase-setq` assigns nil or doesn't touch
> the vars, so this test is not very effective.  I'd rather do:
>
>     (should (equal (list nil nil)
>                    (let ((a 'unset)
>                          (b 'unset))
>                      (pcase-setq `(,a ,b) nil)
>                      (list a b))))

I've also added this to the tests, but what it actually returns
instead...

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

So feel free to adjust the behaviour (or not).

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no




This bug report was last modified 3 years and 278 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.