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: Philip Kaludercic
Subject: Re: Why shouldn't we have a #if .... #else .... #endif construct in Emacs Lisp?
Date: Tue, 29 Aug 2023 12:54:17 +0000

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 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---

> ..  I propose adding it to subr.el, just before (defmacro when ....).
>
> What do people think about this?



reply via email to

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