Next: , Previous: , Up: SRFI syntax   [Contents][Index]


2.12.1 cond-expand (SRFI 0)

SRFI 0 is a mechanism for portably determining the availability of SRFI features. The cond-expand special form conditionally expands according to the features available.

standard special form: cond-expand clause clause dots

Each clause has the form

(feature-requirement expression …)

where feature-requirement can have one of the following forms:

feature-identifier
(and feature-requirement …)
(or feature-requirement …)
(not feature-requirement)
else

(Note that at most one else clause may be present, and it must always be the last clause.)

The cond-expand special form tests for the existence of features at macro-expansion time. It either expands into the body of one of its clauses or signals an error during syntactic processing. cond-expand expands into the body of the first clause whose feature-requirement is currently satisfied (an else clause, if present, is selected if none of the previous clauses is selected).

A feature-requirement has an obvious interpretation as a logical formula, where the feature-identifier variables have meaning true if the feature corresponding to the feature-identifier, as specified in the SRFI registry, is in effect at the location of the cond-expand form, and false otherwise. A feature-requirement is satisfied if its formula is true under this interpretation.

(cond-expand
  ((and srfi-1 srfi-10)
   (write 1))
  ((or srfi-1 srfi-10)
   (write 2))
  (else))

(cond-expand
  (command-line
   (define (program-name) (car (argv)))))

The second example assumes that command-line is an alias for some feature which gives access to command line arguments. Note that an error will be signaled at macro-expansion time if this feature is not present.

Note that MIT/GNU Scheme allows cond-expand in any context where a special form is allowed. This is an extension of the semantics defined by SRFI 0, which only allows cond-expand at top level.


Next: receive (SRFI 8), Previous: SRFI syntax, Up: SRFI syntax   [Contents][Index]