From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: pcase defuns Date: Sun, 19 Dec 2021 12:23:59 -0500 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="9691"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux) Cc: emacs-devel@gnu.org To: Andrew Hyatt Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sun Dec 19 18:25:39 2021 Return-path: Envelope-to: ged-emacs-devel@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 1myzwJ-0002IQ-2d for ged-emacs-devel@m.gmane-mx.org; Sun, 19 Dec 2021 18:25:39 +0100 Original-Received: from localhost ([::1]:48794 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1myzwH-0000vg-Sq for ged-emacs-devel@m.gmane-mx.org; Sun, 19 Dec 2021 12:25:37 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:37532) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1myzuo-0006RL-2O for emacs-devel@gnu.org; Sun, 19 Dec 2021 12:24:06 -0500 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:61810) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1myzul-0007gP-UJ for emacs-devel@gnu.org; Sun, 19 Dec 2021 12:24:05 -0500 Original-Received: from pmg3.iro.umontreal.ca (localhost [127.0.0.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 1A50D441349; Sun, 19 Dec 2021 12:24:02 -0500 (EST) Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 9FB8D4410AD; Sun, 19 Dec 2021 12:24:00 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1639934640; bh=22DA7Ajme6tZTLc+n4qEpTC7FW4aFhqqQKdMi9T6JI8=; h=From:To:Cc:Subject:References:Date:In-Reply-To:From; b=JbmUmrazjDXUCnodDCzlfzvccxdwMg1RpD2YUW4Fj3YAgQQPZSKlGzGAXAEKN2Xx9 2uuX1dWm6RpUKUgkIi+rHX2y2DijQKDJMpfCni1YF/OOb8ooigBI7M+wgih/ZrUb+/ BEZSKXzmhFxKaZYSLPCZLzy3yjk+joXlrQRY2J4J4ltJHWBR2M1D954JhCp9KkSaYi 5Q8DsIbgxArSfSly4kCAzA+6pSZf8ZL8v6tTOaUy5NVYvNZLkbRoIBLLQmddQ2FfJF hjw12OYZw4f7BJG33uChHtmY3mhcTK+rqHGcaQsU0GjIoirVChARfHsQ0EzVOLzs+S WA0lMDGs59PeA== Original-Received: from pastel (unknown [216.154.30.173]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 71914120513; Sun, 19 Dec 2021 12:24:00 -0500 (EST) In-Reply-To: (Andrew Hyatt's message of "Sat, 18 Dec 2021 23:53:10 -0500") Received-SPF: pass client-ip=132.204.25.50; envelope-from=monnier@iro.umontreal.ca; helo=mailscanner.iro.umontreal.ca X-Spam_score_int: -42 X-Spam_score: -4.3 X-Spam_bar: ---- X-Spam_report: (-4.3 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:282458 Archived-At: > As a part of a personal project, I wrote a way to define functions in an > equivalent way to pcases. For example: > > (pcase-defun mytest (a b _) > "Match on 'a 'b with the third argument a wildcard" > "a b match") > > (pcase-defun mytest (c ,var _) > "Match on 'c binding VAR, with the third argument a wildcard" > (format "c %s match" var) ) > > (mytest 'a 'b 'c) -> "a b match" > (mytest 'c 100 'c) -> "c 100 match" > > This is all accomplished by a few small but tricky macros and a hashtable > that holds all the rules. This kind of design crossed my mind a few times but I couldn't come up with a way to give it a reasonable semantics and implementation. Beside the issue of precedence/ordering already mentioned by Tassilo, there's the issue of scoping and order/timing of macroexpansion. E.g.: (let ((x 0)) (pcase-defun mytest (inc-x) (setq x (1+ x))) (pcase-defun mytest (get-x) x)) (let ((y 0)) (pcase-defun mytest (inc-y) (setq y (1+ y))) (pcase-defun mytest (get-y) y)) Does this work "right" with your code? Or: (eval-when-compile (require 'cl-lib)) (pcase-defun foo (..) .. (cl-incf ..) ..) is `cl-incf` properly macroexpanded during compilation of the file, or is it delayed to when the file is loaded, at which point the `cl-incf` macro may be undefined? Stefan