Function: cl-typecase

cl-typecase is an autoloaded macro defined in cl-macs.el.gz.

Signature

(cl-typecase EXPR (TYPE BODY...)...)

Documentation

Eval EXPR and choose among clauses on that value.

Each clause looks like (TYPE BODY...). EXPR is evaluated and, if it satisfies TYPE, the corresponding BODY is evaluated. If no clause succeeds, this macro returns nil. A TYPE of t or otherwise is allowed only in the final clause, and matches if no other keys match.

Aliases

typecase (obsolete since 27.1)

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/cl-macs.el.gz
;;;###autoload
(defmacro cl-typecase (expr &rest clauses)
  "Eval EXPR and choose among clauses on that value.
Each clause looks like (TYPE BODY...).  EXPR is evaluated and, if it
satisfies TYPE, the corresponding BODY is evaluated.  If no clause succeeds,
this macro returns nil.  A TYPE of t or `otherwise' is allowed only in the
final clause, and matches if no other keys match.
\n(fn EXPR (TYPE BODY...)...)"
  (declare (indent 1)
           (debug (form &rest ([&or cl-type-spec "otherwise"] body))))
  (macroexp-let2 macroexp-copyable-p temp expr
    (let* ((type-list nil))
      (cons
       'cond
       (mapcar
        (lambda (c)
          (cons (cond ((eq (car c) 'otherwise) t)
                      ((eq (car c) 'cl--ecase-error-flag)
                       `(error "cl-etypecase failed: %s, %s"
                               ,temp ',(reverse type-list)))
                      (t
                       (push (car c) type-list)
                       `(cl-typep ,temp ',(car c))))
                (or (cdr c) '(nil))))
        clauses)))))