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#70597: Problem in pcase-let? Date: Sat, 04 May 2024 11:05:15 -0400 Message-ID: References: <662d23bf.050a0220.a4e2f.3f45@mx.google.com> <865xvwmus2.fsf@gnu.org> <861q6hhks0.fsf@gnu.org> 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="37146"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: brubar.cs@gmail.com, Eli Zaretskii , 70597-done@debbugs.gnu.org To: Marco Antoniotti Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat May 04 17:05:53 2024 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 1s3GxU-0009NV-Sr for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 04 May 2024 17:05:53 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1s3GxI-0003sY-LI; Sat, 04 May 2024 11:05:40 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s3GxH-0003rO-HY for bug-gnu-emacs@gnu.org; Sat, 04 May 2024 11:05:39 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1s3GxH-0000ux-6X for bug-gnu-emacs@gnu.org; Sat, 04 May 2024 11:05:39 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1s3Gxe-0001oL-7B for bug-gnu-emacs@gnu.org; Sat, 04 May 2024 11:06: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: Sat, 04 May 2024 15:06:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 70597 X-GNU-PR-Package: emacs Original-Received: via spool by 70597-done@debbugs.gnu.org id=D70597.17148351496955 (code D ref 70597); Sat, 04 May 2024 15:06:02 +0000 Original-Received: (at 70597-done) by debbugs.gnu.org; 4 May 2024 15:05:49 +0000 Original-Received: from localhost ([127.0.0.1]:54171 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1s3GxQ-0001o7-QZ for submit@debbugs.gnu.org; Sat, 04 May 2024 11:05:49 -0400 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:5777) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1s3GxO-0001nz-4A for 70597-done@debbugs.gnu.org; Sat, 04 May 2024 11:05:46 -0400 Original-Received: from pmg2.iro.umontreal.ca (localhost.localdomain [127.0.0.1]) by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id A5DAE80919; Sat, 4 May 2024 11:05:17 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1714835116; bh=l0WA9NKXq8G3DNz5J9/cYQi0yjfY2eezrxnsUwcQSi8=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=nHwa39hRC05DmpGSPv7xEF6Hw7jWcyahTek6YkOPp/xkKWHUUi8CDfgeTPIcks5Eg GSQ/5D5xPqQSNhiAJTm1K4dSRrOSpxXyJyXm2qLCGm86nueixVucHFeyFcL37Y2+yg PxW0gTYzDzfRgsN8wxzmwo9A941uBTwzZ5BK2XiGexS0d3gQ75AZ49pWup7Sp403PE QGd3fpZjD1fC2YjrqAuh/38cIz3+KMUHbv/DWZgjkGYDfx3M3SPtiazfgJlG41m2+5 Siw+K8MZmC0CZSLX8t+hui3bAMRAGW2hFIHQ/zE7Mq1iMRgjm+kazOxwfBq0lsxA2H 99rlRXDg96bRg== Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id 3FCB280013; Sat, 4 May 2024 11:05:16 -0400 (EDT) Original-Received: from pastel (unknown [45.72.201.215]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 110AC12032D; Sat, 4 May 2024 11:05:16 -0400 (EDT) In-Reply-To: (Marco Antoniotti's message of "Sat, 4 May 2024 14:06:45 +0200") 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-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:284433 Archived-At: > sorry, but I do not think that the current doc string and/or the > manual explains the situation. Which "current" are you referring to? The "current" manual (in `master`) has a whole section on "Destructuring with pcase pattern" (see below). Do you still see room for misunderstanding there? If so, can you suggest how to change it to make it more clear? Stefan @node Destructuring with pcase Patterns @subsection Destructuring with @code{pcase} Patterns @cindex destructuring with pcase patterns Pcase patterns not only express a condition on the form of the objects they can match, but they can also extract sub-fields of those objects. For example we can extract 2 elements from a list that is the value of the variable @code{my-list} with the following code: @example (pcase my-list (`(add ,x ,y) (message "Contains %S and %S" x y))) @end example This will not only extract @code{x} and @code{y} but will additionally test that @code{my-list} is a list containing exactly 3 elements and whose first element is the symbol @code{add}. If any of those tests fail, @code{pcase} will immediately return @code{nil} without calling @code{message}. Extraction of multiple values stored in an object is known as @dfn{destructuring}. Using @code{pcase} patterns allows you to perform @dfn{destructuring binding}, which is similar to a local binding (@pxref{Local Variables}), but gives values to multiple elements of a variable by extracting those values from an object of compatible structure. The macros described in this section use @code{pcase} patterns to perform destructuring binding. The condition of the object to be of compatible structure means that the object must match the pattern, because only then the object's subfields can be extracted. For example: @example (pcase-let ((`(add ,x ,y) my-list)) (message "Contains %S and %S" x y)) @end example @noindent does the same as the previous example, except that it directly tries to extract @code{x} and @code{y} from @code{my-list} without first verifying if @code{my-list} is a list which has the right number of elements and has @code{add} as its first element. The precise behavior when the object does not actually match the pattern is undefined, although the body will not be silently skipped: either an error is signaled or the body is run with some of the variables potentially bound to arbitrary values like @code{nil}. The pcase patterns that are useful for destructuring bindings are generally those described in @ref{Backquote Patterns}, since they express a specification of the structure of objects that will match. For an alternative facility for destructuring binding, see @ref{seq-let}. @defmac pcase-let bindings body@dots{} Perform destructuring binding of variables according to @var{bindings}, and then evaluate @var{body}. @var{bindings} is a list of bindings of the form @w{@code{(@var{pattern} @var{exp})}}, where @var{exp} is an expression to evaluate and @var{pattern} is a @code{pcase} pattern. All @var{exp}s are evaluated first, after which they are matched against their respective @var{pattern}, introducing new variable bindings that can then be used inside @var{body}. The variable bindings are produced by destructuring binding of elements of @var{pattern} to the values of the corresponding elements of the evaluated @var{exp}. Here's a trivial example: @example (pcase-let ((`(,major ,minor) (split-string "image/png" "/"))) minor) @result{} "png" @end example @end defmac @defmac pcase-let* bindings body@dots{} Perform destructuring binding of variables according to @var{bindings}, and then evaluate @var{body}. @var{bindings} is a list of bindings of the form @code{(@var{pattern} @var{exp})}, where @var{exp} is an expression to evaluate and @var{pattern} is a @code{pcase} pattern. The variable bindings are produced by destructuring binding of elements of @var{pattern} to the values of the corresponding elements of the evaluated @var{exp}. Unlike @code{pcase-let}, but similarly to @code{let*}, each @var{exp} is matched against its corresponding @var{pattern} before processing the next element of @var{bindings}, so the variable bindings introduced in each one of the @var{bindings} are available in the @var{exp}s of the @var{bindings} that follow it, additionally to being available in @var{body}. @end defmac @defmac pcase-dolist (pattern list) body@dots{} Execute @var{body} once for each element of @var{list}, on each iteration performing a destructuring binding of variables in @var{pattern} to the values of the corresponding subfields of the element of @var{list}. The bindings are performed as if by @code{pcase-let}. When @var{pattern} is a simple variable, this ends up being equivalent to @code{dolist} (@pxref{Iteration}). @end defmac @defmac pcase-setq pattern value@dots{} Assign values to variables in a @code{setq} form, destructuring each @var{value} according to its respective @var{pattern}. @end defmac @defmac pcase-lambda lambda-list &rest body This is like @code{lambda}, but allows each argument to be a pattern. For instance, here's a simple function that takes a cons cell as the argument: @example (setq fun (pcase-lambda (`(,key . ,val)) (vector key (* val 10)))) (funcall fun '(foo . 2)) @result{} [foo 20] @end example @end defmac