From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#49809: [PATCH] Add macro 'pcase-setq' Date: Wed, 04 Aug 2021 19:06:11 -0400 Message-ID: References: <07ad5892-3343-4c35-00e3-9753bca5f67b@protonmail.com> Reply-To: Stefan Monnier Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="25592"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) Cc: 49809@debbugs.gnu.org To: Okam Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Aug 05 01:07:11 2021 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mBPyf-0006TK-PM for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 05 Aug 2021 01:07:09 +0200 Original-Received: from localhost ([::1]:60316 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mBPye-00072q-Cn for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 04 Aug 2021 19:07:08 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:50148) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mBPyY-00072T-Mp for bug-gnu-emacs@gnu.org; Wed, 04 Aug 2021 19:07:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:33840) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mBPyY-0000ZK-G8 for bug-gnu-emacs@gnu.org; Wed, 04 Aug 2021 19:07:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mBPyY-0005C2-0n for bug-gnu-emacs@gnu.org; Wed, 04 Aug 2021 19:07:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Stefan Monnier Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 04 Aug 2021 23:07:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 49809 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 49809-submit@debbugs.gnu.org id=B49809.162811838119912 (code B ref 49809); Wed, 04 Aug 2021 23:07:01 +0000 Original-Received: (at 49809) by debbugs.gnu.org; 4 Aug 2021 23:06:21 +0000 Original-Received: from localhost ([127.0.0.1]:45386 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mBPxs-0005B6-VG for submit@debbugs.gnu.org; Wed, 04 Aug 2021 19:06:21 -0400 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:7247) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mBPxr-0005As-LK for 49809@debbugs.gnu.org; Wed, 04 Aug 2021 19:06:20 -0400 Original-Received: from pmg3.iro.umontreal.ca (localhost [127.0.0.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 82CB7441478; Wed, 4 Aug 2021 19:06:13 -0400 (EDT) Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 1082844146D; Wed, 4 Aug 2021 19:06:12 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1628118372; bh=67Jsz4hiqBO8mTq5gDSu3cCZFKwGdnczspAMtyeokeI=; h=From:To:Cc:Subject:References:Date:In-Reply-To:From; b=Iig+UzyNr1CPnzfZ5G/bpkdE6YjV4E83Cqo26oXhGMrjd7/xuCsq8YNJzXo12Kgkf jtUYsN4C/IEZXz8Yu15vISkf5nvMeHcahDqfV+NT/hHnw+mhiIoMAHIhj0PigJ5sUv 1EW/4QzS/yxkE34xHMztLhra/3J0AiFfH8ficFM/v3aCX6CByyJpQCcG9o+0YWFFqF uiA0A2FiGHRnUoyjwfz4xx00ULB3tOLzkRdBVuGeOEzPU5beMo0lIdjTj4gC8xLMhv qCVIV1If4r7gkdAB6OwVoCGJjjO2cMoa0+8UDAqt6sx5bt0/gK5VsIrVyO8bpEfMJb SIzCtVyIngUrg== Original-Received: from alfajor (unknown [216.154.29.138]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id DA7CF120371; Wed, 4 Aug 2021 19:06:11 -0400 (EDT) In-Reply-To: <07ad5892-3343-4c35-00e3-9753bca5f67b@protonmail.com> (Okam's message of "Sun, 01 Aug 2021 17:20:41 +0000") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:211193 Archived-At: > 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