Skip to content

Tutorial 2: Destructuring Argument Specifications

Mark Cox edited this page Sep 15, 2017 · 4 revisions

This tutorial covers the macro destructuring-argument-specification. It provides a convenient way to destructure a function argument specification.

Before outlining the macro, we first define an argument specification.

argspec::= (typespec* [&rest typespec] [&key (keyword typespec)])

where typespec is a common lisp type specifier.

Macro: DESTRUCTURING-ARGUMENT-SPECIFICATION

Syntax:

destructuring-argument-specification lambda-list expression declaration* form*

=> result*

Arguments and Values:

lambda-list -- a argument specification lambda list.

expression -- a form which evaluates to an argument specification.

declaration -- a declare expression; not evaluated.

forms -- an implicit progn.

results -- the values returned by forms.

Argument Specification Lambda List

The argument specification lambda list specifies how an argument specification is to be split in to its constituents. It has the following syntax

lambda-list::= (wholevar reqvars optvars {restvar | keyvars})

wholevar::= [&whole var]

reqvars::= var*

optvars::= [&optional {var | (var [init-form [supplied-p-parameter]])}*]

restvar::= [&others var &rest var]

keyvars::= [&key {var | ({var | (keyword var)}
                         [init-form [supplied-p-parameter]])
                 }*
            [&allow-other-keys]]

&others lambda list keyword

The optional and keyword sections of an argument specification lambda list are similar to the respective sections in the destructuring lambda list used by destructuring-bind.

The &others and &rest lambda list keywords perform a different function. This is shown in the following example.

(let ((argspec '(bit integer string &rest double-float)))
  (destructuring-argument-specification (a &others others &rest rest) argspec
    (values a others rest)))
;; => a
;;    (integer string)
;;    double-float

The value of the &rest var is the type specified in the &rest constituent of the argument specification.

The &others var contains the other required arguments present in the argument specification.

Clone this wiki locally