Subject: bug#72344: [PATCH] Add a version of cl-once-only which handles lists of forms

From: Sean Whitton <[email-masked]>
Date: Sat, 03 Aug 2024 10:51:43 +0800

Hello,

On Mon 29 Jul 2024 at 09:54pm +02, Thuna wrote:

> `cl-once-only*' is meant to work in situations where `cl-once-only' does
> not, namely a situation where you do not know the number of forms to be
> evaluated.
>
> While such a situation *could* exist without, the cases which benefit
> from `cl-once-only*' are overwhelmingly likely to be of the
>
>   (defmacro _ (control &rest args)
>     `(progn
>        (do-something-with-control ,@args)
>        (foo ,@args)))
>
> variety.  The problem here is that the forms in ARGS will be evaluated
> twice while we only want it to happen once.  An alternative way to
> achieve this would be to do
>
>   (defmacro _ (control &rest args)
>     (cl-with-gensyms (argslist)
>       `(let ((,argslist (list ,@args)))
>          ...)))
>
> and either let-bind ARGS to ARGSLIST or only refer to ARGSLIST within
> the body, keeping in mind that ARGSLIST is only a symbol during
> macroexpansion and that in order to refer to individual arguments you
> need to do (nth N ,ARGSLIST) such that the expanded code itself finds
> the relevant argument among all the ones.
>
> With `cl-once-only*', ARGSLIST (or rather ARGS) is not a symbol but a
> list of symbols so to refer to the individual argument you do ,(nth N
> ARGS) and you can simply wrap your macro which worked (sans the double
> evaluating) without the `cl-once-only*' and it should simply* work out of
> the box.

Thanks.  If you have indeed been coming across this in practice, then it
seems worth adding a macro like this.

Btw, I think we would want to see an update to cl.texi along with your
change.

> I don't necessarily think that that name is bad but it doesn't quite get
> to the heart of the macro either.
>
> The macro is meant to be a `cl-once-only' which works on a list of
> forms, and I feel that the "of forms" part is more important than the
> "list" part, which is conceptually more likely to take on a meaning of
> some ambiguous "collection" rather than a chain of cons cells.

I think we should reserve the starred name for now.  As you said, your
macro doesn't come up often, and we might later come up with a variant
of cl-once-only that we want to use very often.

cl-seq-once-only ?

-- 
Sean Whitton