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: A function to take the regexp-matched subsring directly Date: Sun, 30 Oct 2022 18:01:43 -0400 Message-ID: References: <47fff48c-90d4-7c6b-7b92-8a99d9453f3f@gmail.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="20457"; 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: daanturo Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sun Oct 30 23:03:02 2022 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 1opGOS-00057m-C5 for ged-emacs-devel@m.gmane-mx.org; Sun, 30 Oct 2022 23:03:00 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1opGNd-0005vq-GE; Sun, 30 Oct 2022 18:02:12 -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 1opGNU-0005tF-2V for emacs-devel@gnu.org; Sun, 30 Oct 2022 18:02:01 -0400 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1opGNS-0007rW-8S for emacs-devel@gnu.org; Sun, 30 Oct 2022 18:01:59 -0400 Original-Received: from pmg2.iro.umontreal.ca (localhost.localdomain [127.0.0.1]) by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id 56FA780325; Sun, 30 Oct 2022 18:01:56 -0400 (EDT) Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id 6A23080667; Sun, 30 Oct 2022 18:01:50 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1667167310; bh=amjrkqGAZP4t9FjfCryzdyZGMzgdJdKgKollROs219I=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=jLgEaT4CXu9hT4pnsAHQyRcZAjsbFT/f+VW3y/EzA1pvxye3HWV0rhXY0wwSeUmB1 3lFTIXL9Yf/U4CJR4mkrWCVKwubn2tDv48K1VF0y+5p8isHZf/Gh4t1rpdB3wkJTww JKpwuyOZFvuqukSiicbEKeUO1PRHysQJj01hPIG9u+tkqKVMCdIhBPo5r/2EaADoxK ejNW7qqQqxggTeGXvO1tdQDxgc1X0Lzq9Ehb7wDkeA5yezHYdzPBmTnTmaqyx+srLg 6ybuzZkVscbPA8jE6xZpioZTEVtkAH2VO6nu7MRtWPkG+PhyRogSFr7f5ZxNX6WJOl DCPLDDphNak5A== Original-Received: from pastel (65-110-220-202.cpe.pppoe.ca [65.110.220.202]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id D4F4E12020D; Sun, 30 Oct 2022 18:01:49 -0400 (EDT) In-Reply-To: <47fff48c-90d4-7c6b-7b92-8a99d9453f3f@gmail.com> (daanturo@gmail.com's message of "Mon, 31 Oct 2022 00:16:03 +0700") 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: , Original-Sender: "Emacs-devel" Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:298831 Archived-At: >> `save-match-data` is costly and extremely rarely needed. > > I committed a change that now makes inhibit-modify optional (though `(declare > (pure t) (side-effect-free t))` is lost in the process). Optional makes no sense: those who need the match data to be saved can use `save-match-data` around the call just as easily as passing an optional argument. > Although I think intuitively, when running those kind of functions, we > naturally expect them not to cause any side-effects from a high-level > perspective so `save-match-data` should be the default. That's not how it works: your intuition should say "oh, it uses a regexp, so it most assuredly messes with the match data". Only very few primitive operations like `car/cdr` preserve the match data. Everything else should be presumed to mess with the match data. `save-match-data` should almost never be used at the top-level of a function. It should only be used in cases such as: ... (string-match ..) ... (save-match-data ...do something that may mess with the match data...) ... (match-string ..) I tried to explain that in the docstring as follow: NOTE: The convention in Elisp is that any function, except for a few exceptions like car/assoc/+/goto-char, can clobber the match data, so `save-match-data' should normally be used to save *your* match data rather than your caller's match data." -- Stefan