Function With-Variables

Part of:

package metabang.utilities
( with-variables < symbols > &body < body > )
Using gensyms is necessary to prevent variables produced by macro expansions from interfering with user variables, and naming them mnemonically helps make macro expansions and compiled code easier to read, but it's a pain to create them properly. This macro creates them for you, which makes writing nice macros easier. For example, if you are writing a macro to iterate over an array, you used to have to write: (defmacro do-2d-array ((elt array) &body body) (let ((row (gensym “ROW”)) (col (gensym “COL”))) `(dotimes (,row (array-dimension 0)) (dotimes (,col ,(array-dimension 1)) (let ((,elt (aref ,array ,row ,col))) . ,body))))) Now you can just write the following, which eliminates the need to laboriously create the mnemonic gensyms. (defmacro do-2d-array ((elt array) &body body) (with-variables (row col) `(dotimes (,row ,(array-dimension 0)) (dotimes (,col ,(array-dimension 1)) (let ((,elt (aref ,array ,row ,col))) . ,body))))