Package: alexandria

Macro destructuring-ecase

Lambda List

destructuring-ecase (keyform &body clauses)


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


The macro destructuring-ecase is a combination of ecase and destructuring-bind.

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


(defun decase (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))))

(decase (list :foo 1 2)) => "foo: 1, 2" (decase (list :bar :a 1 :b 2)) => "bar: 1, 2" (decase (list :alt1 1)) => "alt: 1" (decase (list :alt2 2)) => "alt: 2" (decase (list :quux 1 2 3)) =| error

See also