[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Why shouldn't we have a #if .... #else .... #endif construct in Emacs Li
From: |
Alan Mackenzie |
Subject: |
Why shouldn't we have a #if .... #else .... #endif construct in Emacs Lisp? |
Date: |
Mon, 28 Aug 2023 19:37:57 +0000 |
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.
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))))
.. I propose adding it to subr.el, just before (defmacro when ....).
What do people think about this?
--
Alan Mackenzie (Nuremberg, Germany).
- Why shouldn't we have a #if .... #else .... #endif construct in Emacs Lisp?,
Alan Mackenzie <=
Re: Why shouldn't we have a #if .... #else .... #endif construct in Emacs Lisp?, Emanuel Berg, 2023/08/28