This causes an error
(cl-destructuring-bind
(&key a b)
'(:a "foo" :b 13 :c "bar")
(list a b))
because the :c
key/value is not handled in the pattern match.
Often I find myself wanting to extract some subset of keys/values from a plist, but cl-destructuring-bind
isn't appropriate because of this limitation.
Is there a way to make cl-destructuring-bind
simply ignore unmatched keys in the EXPR? I'm not sure what the official common lisp behaviour is supposed to be here, is the error in the spec?
Note that the dash library has some support for a similar destructuring with -let
, but that requires some boilerplate to assign a symbol to each key. A fix has been proposed
Taking Stefan's answer into account, this works well:
(defmacro plist-bind (args expr &rest body)
"`destructuring-bind' without the boilerplate for plists."
`(cl-destructuring-bind
(&key ,@args &allow-other-keys)
,expr
,@body))
(plist-bind
(a b)
'(:a "foo" :b 13 :c "bar")
(list a b)) => ("foo" 13)