(cl.info)Macros


Next: Declarations Prev: Control Structure Up: Top

Macros
******

This package implements the various Common Lisp features of `defmacro',
such as destructuring, `&environment', and `&body'.  Top-level `&whole'
is not implemented for `defmacro' due to technical difficulties.  *Note
Argument Lists::.

   Destructuring is made available to the user by way of the following
macro:

 - Special Form: destructuring-bind ARGLIST EXPR FORMS...
     This macro expands to code which executes FORMS, with the
     variables in ARGLIST bound to the list of values returned by EXPR.
     The ARGLIST can include all the features allowed for `defmacro'
     argument lists, including destructuring.  (The `&environment'
     keyword is not allowed.)  The macro expansion will signal an error
     if EXPR returns a list of the wrong number of arguments or with
     incorrect keyword arguments.

   This package also includes the Common Lisp `define-compiler-macro'
facility, which allows you to define compile-time expansions and
optimizations for your functions.

 - Special Form: define-compiler-macro NAME ARGLIST FORMS...
     This form is similar to `defmacro', except that it only expands
     calls to NAME at compile-time; calls processed by the Lisp
     interpreter are not expanded, nor are they expanded by the
     `macroexpand' function.

     The argument list may begin with a `&whole' keyword and a
     variable.  This variable is bound to the macro-call form itself,
     i.e., to a list of the form `(NAME ARGS...)'.  If the macro
     expander returns this form unchanged, then the compiler treats it
     as a normal function call.  This allows compiler macros to work as
     optimizers for special cases of a function, leaving complicated
     cases alone.

     For example, here is a simplified version of a definition that
     appears as a standard part of this package:

          (define-compiler-macro member* (&whole form a list &rest keys)
            (if (and (null keys)
                     (eq (car-safe a) 'quote)
                     (not (floatp-safe (cadr a))))
                (list 'memq a list)
              form))

     This definition causes `(member* A LIST)' to change to a call to
     the faster `memq' in the common case where A is a
     non-floating-point constant; if A is anything else, or if there
     are any keyword arguments in the call, then the original `member*'
     call is left intact.  (The actual compiler macro for `member*'
     optimizes a number of other cases, including common `:test'
     predicates.)

 - Function: compiler-macroexpand FORM
     This function is analogous to `macroexpand', except that it
     expands compiler macros rather than regular macros.  It returns
     FORM unchanged if it is not a call to a function for which a
     compiler macro has been defined, or if that compiler macro decided
     to punt by returning its `&whole' argument.  Like `macroexpand',
     it expands repeatedly until it reaches a form for which no further
     expansion is possible.

   Note: Macro Bindings, for descriptions of the `macrolet' and
`symbol-macrolet' forms for making "local" macro definitions.


automatically generated by info2www