Next: , Previous: About SRFI Usage, Up: SRFI Support


6.4.2 SRFI-0 - cond-expand

This SRFI lets a portable Scheme program test for the presence of certain features, and adapt itself by using different blocks of code, or fail if the necessary features are not available. There's no module to load, this is in the Guile core.

A program designed only for Guile will generally not need this mechanism, such a program can of course directly use the various documented parts of Guile.

— syntax: cond-expand (feature body...) ...

Expand to the body of the first clause whose feature specification is satisfied. It is an error if no feature is satisfied.

Features are symbols such as srfi-1, and a feature specification can use and, or and not forms to test combinations. The last clause can be an else, to be used if no other passes.

For example, define a private version of alist-cons if SRFI-1 is not available.

          (cond-expand (srfi-1
                        )
                       (else
                        (define (alist-cons key val alist)
                          (cons (cons key val) alist))))
     

Or demand a certain set of SRFIs (list operations, string ports, receive and string operations), failing if they're not available.

          (cond-expand ((and srfi-1 srfi-6 srfi-8 srfi-13)
                        ))
     

The Guile core has the following features,

     guile
     r5rs
     srfi-0
     srfi-4
     srfi-6
     srfi-13
     srfi-14

Other SRFI feature symbols are defined once their code has been loaded with use-modules, since only then are their bindings available.

The `--use-srfi' command line option (see Invoking Guile) is a good way to load SRFIs to satisfy cond-expand when running a portable program.

Testing the guile feature allows a program to adapt itself to the Guile module system, but still run on other Scheme systems. For example the following demands SRFI-8 (receive), but also knows how to load it with the Guile mechanism.

     (cond-expand (srfi-8
                   )
                  (guile
                   (use-modules (srfi srfi-8))))

It should be noted that cond-expand is separate from the *features* mechanism (see Feature Tracking), feature symbols in one are unrelated to those in the other.