From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Robin Tarsiger Newsgroups: gmane.emacs.devel Subject: Re: defmacro with built-in gensym declaration and initialization Date: Wed, 20 Jan 2021 10:47:01 -0600 Message-ID: <1ca86a74-4762-e63b-4196-ea34a1fec21d@dasyatidae.com> References: <87bldk6n2l.fsf@gmail.com> <87turbiuvr.fsf@tcd.ie> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="24134"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 Cc: emacs-devel@gnu.org To: Stefan Monnier , akater Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Wed Jan 20 17:50:21 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 1l2GgV-00069P-Qy for ged-emacs-devel@m.gmane-mx.org; Wed, 20 Jan 2021 17:50:19 +0100 Original-Received: from localhost ([::1]:53564 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l2GgU-0005ri-AG for ged-emacs-devel@m.gmane-mx.org; Wed, 20 Jan 2021 11:50:18 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:39096) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l2GdK-0004IR-Fs for emacs-devel@gnu.org; Wed, 20 Jan 2021 11:47:04 -0500 Original-Received: from out3-smtp.messagingengine.com ([66.111.4.27]:52255) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l2GdF-0005sI-8S for emacs-devel@gnu.org; Wed, 20 Jan 2021 11:47:02 -0500 Original-Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id B60DC5C01EA; Wed, 20 Jan 2021 11:46:54 -0500 (EST) Original-Received: from mailfrontend2 ([10.202.2.163]) by compute1.internal (MEProxy); Wed, 20 Jan 2021 11:46:54 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dasyatidae.com; h=to:cc:references:from:subject:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=i vi3oCs5HDOQUcI+k84PBMhdbVA9+3eeRArc96HTXps=; b=qtmE1O58o/rl2S1+3 VO2RaJ4XfgfLyGB336r2Fj//3wXrIoFHTkfSTuSXIK0Dli2m7sN3A2yfkDzI5vN0 b9JeY0tnOhp+jGUa2T6d4BtHAsPqGstWAgFLUBBD9sl7dKs4DSP+VLm0NMrx9n+b Sa8EthEgJcvOn7KqxV9tHhwR6BB9iAkCqOOcRAaDp+dZb7YVXsQHhoY+TTzMnJ1l EmS3FazNEiGmjBDCJ9AIukdocnyRx1Vc4Q9ZAw7SZmBqBRLtW1J62/lnZb+6Qk15 D/RuJ6H3v+js3kDgV43gWCBBDhzkmPJiID+I+hZwAShZqo40oCAWrwbIU6J1riJP rgMZw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=ivi3oCs5HDOQUcI+k84PBMhdbVA9+3eeRArc96HTX ps=; b=YAoc0fGxF9Hgp43jVmxrXt/SqwxUC2CVgBUXg1OCNnNqL96EFQMeAZ8lK pHw8AS5gkeJeOhkLZB3vAVGY/bzHOQ72EvOdxd7Ead787G2E0tI1rbKdrgRJrltq /jU/zxkMlmHu8HUk6nG2sRaAGOU22Mo9w5kVS9w0K/okFAKOINmo676uYyVuU324 GvO9TIiTlmmKGsUIDUuwe4OWC7MJ0kPOvul/1HJJiQxDL1ldy8BtRZM8fA8Tl43R Duwc7m66AjpbyDwvpWZT0G/9IjCPXOLQzZjI4Hvi1LiIxrbZBx5aS/gcvpP89ErF FvevjuMKD0jGeD8I3k/Zw9GmtvZ8g== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledruddvgdelgecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefvfhfhuffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeftohgsihhn ucfvrghrshhighgvrhcuoehrthhtsegurghshigrthhiuggrvgdrtghomheqnecuggftrf grthhtvghrnhepteejgefghfffieegffehkeelleeivdeghfevteduvedttefhfedthfff uedviedtnecuffhomhgrihhnpegtohhmmhhonhdqlhhishhprdhnvghtnecukfhppeejie drvdehfedrjeehrdefgeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgr ihhlfhhrohhmpehrthhtsegurghshigrthhiuggrvgdrtghomh X-ME-Proxy: Original-Received: from [192.168.1.65] (76-253-75-34.lightspeed.austtx.sbcglobal.net [76.253.75.34]) by mail.messagingengine.com (Postfix) with ESMTPA id 2DA1E1080067; Wed, 20 Jan 2021 11:46:54 -0500 (EST) In-Reply-To: Content-Language: en-US-large Received-SPF: pass client-ip=66.111.4.27; envelope-from=rtt@dasyatidae.com; helo=out3-smtp.messagingengine.com X-Spam_score_int: -28 X-Spam_score: -2.9 X-Spam_bar: -- X-Spam_report: (-2.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, NICE_REPLY_A=-0.094, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-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.23 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:263212 Archived-At: Stefan Monnier wrote: > Indeed, `macroexp-let2` was designed for similar situations. > I'm not completely happy with it either, so if someone can design > something better, that'd be welcome, FWIW, I'm generally happy with the conveniences the Alexandria library provides for in CL: https://common-lisp.net/project/alexandria/draft/alexandria.html#Macro-Writing once-only is similar to macroexp-let2* but cleaner and more convenient (though it also acts like let rather than let*). Ad-hoc example: (defmacro strange-hypot (x y) (once-only (x y (z '(current-z-difference))) `(+ (* ,x ,x) (* ,y ,y) (* ,z ,z))) (macroexpand '(strange-hypot (+ x dx) 42)) ==> (LET ((#:X634 (+ X DX)) (#:Y635 42) (#:Z636 (CURRENT-Z-DIFFERENCE))) (+ (* #:X634 #:X634) (* #:Y635 #:Y635) (* #:Z636 #:Z636))) Note that the "single evaluation as expanded of one macro argument" case is very convenient, which is a big step up from Emacs's macroexp- forms. with-gensyms is closer to what akater was describing earlier, and is a "lower-level" helper around let and gensym. Ad-hoc example: (defmacro vec3+nums-macro (v1 x2 y2 z2) (with-gensyms (v1-var x-var y-var z-var) `(let* ((,v1-var ,v1) (,x-var (+ ,x2 (elt ,v1-var 0))) (,y-var (+ ,y2 (elt ,v1-var 1))) (,z-var (+ ,z2 (elt ,v1-var 2)))) (vec3 ,x-var ,y-var ,z-var)))) (macroexpand '(vec3+nums-macro (acquire-vector (somehow)) (+ previous-x dx) y -1.0)) ==> (LET* ((#:V1-VAR637 (ACQUIRE-VECTOR (SOMEHOW))) (#:X-VAR638 (+ (+ PREVIOUS-X DX) (ELT #:V1-VAR637 0))) (#:Y-VAR639 (+ Y (ELT #:V1-VAR637 1))) (#:Z-VAR640 (+ -1.0 (ELT #:V1-VAR637 2)))) (VEC3 #:X-VAR638 #:Y-VAR639 #:Z-VAR640)) Note that the let* form is not convenient in once-only, and the Alexandria helpers don't do any of the constant-related optimization that Emacs's do AFAIK, but the possible slight adjustments there are fairly obvious. -RTT