From: Robin Tarsiger <rtt@dasyatidae.com>
To: Stefan Monnier <monnier@iro.umontreal.ca>,
akater <nuclearspace@gmail.com>
Cc: emacs-devel@gnu.org
Subject: Re: defmacro with built-in gensym declaration and initialization
Date: Wed, 20 Jan 2021 10:47:01 -0600 [thread overview]
Message-ID: <1ca86a74-4762-e63b-4196-ea34a1fec21d@dasyatidae.com> (raw)
In-Reply-To: <jwveeif641k.fsf-monnier+emacs@gnu.org>
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
next prev parent reply other threads:[~2021-01-20 16:47 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-01-20 8:15 defmacro with built-in gensym declaration and initialization akater
2021-01-20 13:46 ` Basil L. Contovounesios
2021-01-20 15:09 ` Stefan Monnier
2021-01-20 16:47 ` Robin Tarsiger [this message]
2021-01-20 19:28 ` akater
2021-01-20 20:55 ` Basil L. Contovounesios
2021-01-21 19:34 ` akater
2021-01-21 20:50 ` Stefan Monnier
2021-01-21 20:50 ` Basil L. Contovounesios
2021-01-21 21:05 ` Stefan Monnier
2021-01-21 21:23 ` Basil L. Contovounesios
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1ca86a74-4762-e63b-4196-ea34a1fec21d@dasyatidae.com \
--to=rtt@dasyatidae.com \
--cc=emacs-devel@gnu.org \
--cc=monnier@iro.umontreal.ca \
--cc=nuclearspace@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).