emacs-devel
[Top][All Lists]
Advanced

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

Re: Why shouldn't we have a #if .... #else .... #endif construct in Emac


From: Alan Mackenzie
Subject: Re: Why shouldn't we have a #if .... #else .... #endif construct in Emacs Lisp?
Date: Tue, 29 Aug 2023 13:23:59 +0000

Hello, Philip.

On Tue, Aug 29, 2023 at 12:54:17 +0000, Philip Kaludercic wrote:
> Alan Mackenzie <acm@muc.de> writes:

> > Hello, Emacs.

> > In C, we have the very useful conditional compilation directives
> > introduced by #if or #ifdef, etc., which end at #end.

> > In Emacs Lisp we have no such construct.  This is a Bad Thing.

> > More and more, especially recently, irritating warning messages are
> > occurring for, for example, obsolete variables and functions inside
> > conditionals which ensure they aren't used.  For example:

> >     (when (< emacs-major-version 24)
> >       (defadvice .....))

> > produces the warning about defadvice being obsolete.  (I haven't actually
> > tested this example).  What we really want here is for the defadvice only
> > to be _compiled_ when (< emacs-major-version 24), rather than compiled
> > unconditionally and not run.

> In this specific case, would it be possible to use the nadvice
> compatibility package on GNU ELPA?

I suspect it would be, yes.

> > I propose a new function, hash-if, which would do what we want.  The
> > above example could then be written something like:

> >     (hash-if (< emacs-major-version 24)
> >         (defadvice .....)
> >       (advice-add .....))

> > ..  This is not actually all that difficult to write.  My first attempt
> > uses a compiler-macro, and looks like this:

> >     (defun hash-if (condition if-part &rest else-part)
> >       "A compiler macro analogous to C's #if.
> >     CONDITION is evaluated at compile time.  If it is non-nil,
> >     IF-PART gets compiled.  Otherwise ELSE-PART (enclosed in a
> >     `progn') gets compiled."
> >       (declare (indent 2))
> >       (error "hash-if has been called directly"))

> >     (put 'hash-if 'compiler-macro
> >          (lambda (form condition if-part &rest else-part)
> >            (if (eval condition lexical-binding)
> >                if-part
> >              (cons 'progn else-part))))

> Would something like work as well:

> --8<---------------cut here---------------start------------->8---
> (defmacro cif (test then &rest else)
>   "Evaluate TEST during macro-expansion and return THEN or ELSE."
>   (if (eval test t) then else))
> --8<---------------cut here---------------end--------------->8---

Hah!  Trust me to build something twisted and complicated when there's a
simple solution which will do just as well!

I think the `else' needs a 'progn, though, like this:

    (if (eval test t) then (cons 'progn else))

, and the eval form probably wants to go into a condition-case for
Emacsen lacking the second parameter.  But I appreciate you put the code
together quickly, rather than working out every last detail.  Thanks!

> > ..  I propose adding it to subr.el, just before (defmacro when ....).

> > What do people think about this?

Yes, what do people think about the idea?

-- 
Alan Mackenzie (Nuremberg, Germany).



reply via email to

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