Destructuring Bind
… binds the variables specified in lambda-list to the corresponding values in the tree structure resulting from the evaluation of expression; then destructuring-bind evaluates forms.
‘destructuring-bind‘ is a wonderful macro. The first thing you do is hand it a lambda list and a list of values. It acts like a pattern matcher. If the list of values does not match up to the lambda list it will throw an error.
Here is what I’m talking about:
(defun test-d-bind (&rest args) (destructuring-bind (a &optional (b 3) &rest x &key c (d a)) args (list a b c d x))
(test-d-bind nil) ;>>(NIL 3 NIL NIL NIL) (test-d-bind 1 6 :d 8 :c 9 :d 10) ;>>(1 6 9 8 (:D 8 :C 9 :D 10)
Parameter Keywords
A macro lambda list can contain the lambda list keywords:
&allow-other-keys &environment
&rest &aux
&key &whole
&body &optional
An ordinary lambda list can contain the lambda list keywords:
&allow-other-keys &key
&rest &aux &optional
The object framework that I’ve been covering couldn’t handle parameter keywords. Now it can. This is all due to destructuring-bind. A very universal macro. I believe that because of this macro I will be able to add generic function for all objects and also multiple function signatures.
(defmacro make-property (name args) (let ((g (gensym)) (r-args (remove-if-not-symbol args))) (multiple-value-bind (sym-name key-name) (symbol-and-keyword name) `(progn (defun ,sym-name (obj &rest ,g) (block ,sym-name (destructuring-bind ,args ,g (declare (ignorable ,@r-args)) (apply (getf obj ,key-name) ,g)))))))
Future speculation
Generic function and multiple function signatures can be derived from the above code. Looking at the above code we see that the destructuring-bind is embedded within the defun block. Right now we overwrite any previously defun-ed function.
- store ‘args into a list, then using name as key store the list into a hash (args-template table). The ‘args will be considered an instance of a template.
- wrap an error-handler around destructuring-bind and then iterate through the templates to see if a match. If not add to templates table.
Happy Snow Days!