Function: and-let*

and-let* is a macro defined in subr.el.gz.

Signature

(and-let* VARLIST &rest BODY)

Documentation

Bind variables according to VARLIST and conditionally evaluate BODY.

Evaluate each binding in turn, stopping if a binding value is nil. If all bindings are non-nil, evaluate the forms in BODY and return the value of the last form, or else the last binding value if BODY is empty.

Like when-let*, except for the handling of an empty BODY.

Some Lisp programmers follow the convention that and and and-let* are for forms evaluated for return value, and when and when-let* are for forms evaluated for side-effect with returned values ignored.

View in manual

Probably introduced at or before Emacs version 31.1.

Source Code

;; Defined in /usr/src/emacs/lisp/subr.el.gz
(defmacro and-let* (varlist &rest body)
  "Bind variables according to VARLIST and conditionally evaluate BODY.
Evaluate each binding in turn, stopping if a binding value is nil.
If all bindings are non-nil, evaluate the forms in BODY
and return the value of the last form, or else the last binding value
if BODY is empty.

Like `when-let*', except for the handling of an empty BODY.

Some Lisp programmers follow the convention that `and' and `and-let*'
are for forms evaluated for return value, and `when' and `when-let*' are
for forms evaluated for side-effect with returned values ignored."
  ;; ^ Document this convention here because it explains why we have
  ;;   both `when-let*' and `and-let*' (in addition to the additional
  ;;   feature of `and-let*' when BODY is empty).
  (declare (indent 1) (debug if-let*))
  (let (res)
    (if varlist
        `(let* ,(setq varlist (internal--build-bindings varlist))
           (when ,(setq res (caar (last varlist)))
             ,@(or body `(,res))))
      `(let* () ,@(or body '(t))))))