Function: if-let

if-let is a macro defined in subr.el.gz.

Signature

(if-let SPEC THEN &rest ELSE)

Documentation

Bind variables according to SPEC and evaluate THEN or ELSE.

This is like if-let* except, as a special case, interpret a SPEC of the form (SYMBOL SOMETHING) like ((SYMBOL SOMETHING)). This exists for backward compatibility with an old syntax that accepted only one binding.

This macro will be marked obsolete in Emacs 31.1; prefer if-let* in new code.

Probably introduced at or before Emacs version 25.1.

Source Code

;; Defined in /usr/src/emacs/lisp/subr.el.gz
(defmacro if-let (spec then &rest else)
  "Bind variables according to SPEC and evaluate THEN or ELSE.
This is like `if-let*' except, as a special case, interpret a SPEC of
the form \(SYMBOL SOMETHING) like \((SYMBOL SOMETHING)).  This exists
for backward compatibility with an old syntax that accepted only one
binding.

This macro will be marked obsolete in Emacs 31.1; prefer `if-let*' in
new code."
  (declare (indent 2)
           (debug ([&or (symbolp form)  ; must be first, Bug#48489
                        (&rest [&or symbolp (symbolp form) (form)])]
                   body)))
  (when (and (<= (length spec) 2)
             (not (listp (car spec))))
    ;; Adjust the single binding case
    (setq spec (list spec)))
  (list 'if-let* spec then (macroexp-progn else)))