Introduce if! & when! macros

The condition argument is evaluated at compile/expansion time, and its
body expanded directly.
This commit is contained in:
Henrik Lissner 2020-04-29 21:09:10 -04:00
parent d12752324a
commit 936124e546
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
2 changed files with 19 additions and 2 deletions

View file

@ -240,6 +240,23 @@ writes to `standard-output'."
(save-silently t))
(prog1 ,@forms (message ""))))))
(defmacro if! (cond then &rest body)
"Expands to THEN if COND is non-nil, to BODY otherwise.
COND is checked at compile/expansion time, allowing BODY to be omitted
entirely when the elisp is byte-compiled. Use this for forms that contain
expensive macros that could safely be removed at compile time."
(declare (indent 2))
(if (eval cond)
then
(macroexp-progn body)))
(defmacro when! (cond &rest body)
"Expands to BODY if CONDITION is non-nil at compile/expansion time.
See `if!' for details on this macro's purpose."
(declare (indent 1))
(when (eval cond)
(macroexp-progn body)))
;;; Mutation
(defmacro appendq! (sym &rest lists)