GNU bug report logs -
#76132
Clojure-style auto-gensyms for macros
Previous Next
Reported by: Tassilo Horn <tsdh <at> gnu.org>
Date: Fri, 7 Feb 2025 21:13:02 UTC
Severity: wishlist
Tags: patch
Done: Tassilo Horn <tsdh <at> gnu.org>
Bug is archived. No further changes may be made.
Full log
View this message in rfc822 format
> Clojure has a very convenient feature to make that easy. While you can
> write such macros traditionally like
>
> (defmacro foo [x y]
> (let [xv (gensym "x")
> yv (gensym "y")]
> `(let [,xv ,x
> ,yv ,y]
> (do-stuff (* ,xv ,xv) (* ,yv ,yv)))))
>
> you can also write much more concise and convenient
> > you can also write much more concise and convenient
>
> (defmacro foo [x y]
> `(let [xv# ,x
> yv# ,y]
> (do-stuff (* xv# xv#) (* yv# yv#))))
>
> where each symbol ending in # will be replaced
> by a unique gensymed symbol (per name).
> Would there be interest in adding something
> like that to Elisp?
So you could no longer let-bind a variable
whose name ends in `#', to get a normal let
binding? (Admittedly, you need to write that
as `\#' in the source code.)
Doesn't sound like an improvement, to me.
A priori, I'm not in favor of limiting the
names you can use for variables.
That would be especially pernicious with let
bindings of dynamic ("special") variables.
If it were limited to lexical variables it
wouldn't be so bad. We should be able to
bind dynamic vars whose names end with `#'.
(Again though, admittedly the `#' chars need
to be escaped in source code: `\#'.)
The cliché of handling this kind of thing in
macros with gensym is pretty standard (it may
even be the main use of gensym). Yes, it can
be error prone, but it's not really harder to
use gensym than it is to add `#' to var names.
And the body is IMO clearer without the added
`#'s and subtracted commas.
There might be some other way to provide such
a shortcut, without removing the _general_
possibility of naming let variables with `#'
at the end. A new variety (yet another?) of
`let' perhaps, that does what you suggest.
I'll note too that Common Lisp doesn't bother
with such things either. And people have been
writing Lisp macros with Common Lisp and its
ancestors for many, many moon.
This bug report was last modified 148 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.