Package: alexandria

Macro destructuring-case

Lambda List

destructuring-case (keyform &body clauses)


clauses ::= ((case-keys . destructuring-lambda-list) form*)


The macro destructuring-case is a combination of the Common Lisp macros case and destructuring-bind.

keyform must evaluate to a cons. The clause whose case-keys matches car of key, as if by the Common Lisp macro case is selected, and the forms are then executed with the cdr of key is destructured and bound by the destructuring-lambda-list.


(defun dcase (x)
  (destructuring-case x
    ((:foo a b)
     (format nil "foo: ~S, ~S" a b))
    ((:bar &key a b)
     (format nil "bar, ~S, ~S" a b))
    (((:alt1 :alt2) a)
     (format nil "alt: ~S" a))
    ((t &rest rest)
     (format nil "unknown: ~S" rest))))

(dcase (list :foo 1 2)) => "foo: 1, 2" (dcase (list :bar :a 1 :b 2)) => "bar: 1, 2" (dcase (list :alt1 1)) => "alt: 1" (dcase (list :alt2 2)) => "alt: 2" (dcase (list :quux 1 2 3)) => "unknown: 1, 2, 3"

See also