emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Macros considered harmful


From: Stefan Monnier
Subject: Re: Macros considered harmful
Date: Tue, 06 Sep 2022 12:01:01 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux)

>> Admittedly, another way around these kinds of problems is to teach the
>> compiler how to deal with an unknown macro.  I.e. something like
>> (declare-macro my-foo ...) so that if the compiler see (my-foo ...) but
>> `my-foo` can't be macroexpanded (because the macro is not yet defined),
>> it doesn't incorrectly compile it into a function call, but instead
>> residualizes it into something like a call to `eval`.  Making it
>> interact correctly with lexical scoping could be tricky (I guess the
>> simplest solution would be to residualize the whole toplevel expression
>> in which the macro call was found).

A low-tech way to do it is to let the programmer do it by hand, e.g.:

    (defmacro smalltalk--when-fboundp (sym exp)
      (declare (indent 1) (debug (symbolp form)))
      (if (fboundp sym)
          exp
        ;; `sym' is not defined during compilation, but keep the test at 
run-time,
        ;; in case we use the compiled file on a newer Emacs.
        `(eval '(if (fboundp ',sym) ,exp))))

It can still break if you use in `exp` lexically scoped vars declared in
the context, but that's considered a "programmer's problem" :-(

> Another downside of macros not directly addressed by this approach is
> that packages using them may have the outrageous desire to both support
> older Emacsen and build cleanly, at the same time!  Recall, for example,
> this unresolved shortdoc thread:
> https://lists.gnu.org/r/emacs-devel/2021-09/msg01719.html

Would this kind of `<foo>--when-fboundp` help there?


        Stefan




reply via email to

[Prev in Thread] Current Thread [Next in Thread]