Non-standard macros and special forms

Making extra libraries and extensions available

require-library

[syntax] (require-library ID ...)

This form does all the necessary steps to make the libraries or extensions given in ID ... available. It loads syntactic extensions, if needed and generates code for loading/linking with core library modules or separately installed extensions.

During interpretation/evaluation require-library performs one of the following:

If ID names one of the syntactic extensions chicken-syntax chicken-ffi-syntax, then this extension will be loaded.

If ID names one of the core library units shipped with CHICKEN, then a (load-library 'ID) will be performed.

If ID names an installed extension with the syntax or require-at-runtime attribute, then the extensions is loaded at compile-time, probably doing a run-time (require ...) for any run-time requirements.

If ID names one of the syntactic extensions chicken-syntax chicken-ffi-syntax, then this extension will be loaded at compile-time, making the syntactic extensions available in compiled code.

If ID names one of the core library units shipped with CHICKEN, or if the option -uses ID has been passed to the compiler, then a (declare (uses ID)) is generated.

If ID names an installed extension with the syntax or require-at-runtime attribute, then the extension is loaded at compile-time, and code is emitted to (require ...) any needed run-time requirements.

Otherwise (require-library ID) is equivalent to (require 'ID).

To make long matters short - just use require-library and it will normally figure everything out for dynamically loadable extensions and core library units.

ID should be a pure extension name and should not contain any path prefixes (for example dir/lib...) is illegal).

ID may also be a list that designates an extension-specifier. Currently the following extension specifiers are defined:

(srfi NUMBER ...) is required for SRFI-55 compatibility and is fully implemented

(version ID NUMBER) is equivalent to ID, but checks at compile-time whether the extension named ID is installed and whether its version is equal or higher than NUMBER. NUMBER may be a string or a number, the comparison is done lexicographically (using string>=?).

See also: set-extension-specifier!

require-extension

[syntax] (require-extension ID ...)

This is equivalent to (require-library ID ...) but performs an implicit import, if necessary. This implementation of require-extension is compliant with SRFI-55 (see the SRFI-55 document for more information).

use

[syntax] (use ID ...)

use is just a shorter alias for require-extension.

Binding forms for optional arguments

optional

[syntax] (optional ARGS DEFAULT)

Use this form for procedures that take a single optional argument. If ARGS is the empty list DEFAULT is evaluated and returned, otherwise the first element of the list ARGS. It is an error if ARGS contains more than one value.

let-optionals

[syntax] (let-optionals ARGS ((VAR1 DEFAULT1) ...) BODY ...)

Binding constructs for optional procedure arguments. ARGS should be a rest-parameter taken from a lambda-list. let-optionals binds VAR1 ... to available arguments in parallel, or to DEFAULT1 ... if not enough arguments were provided. let-optionals* binds VAR1 ... sequentially, so every variable sees the previous ones. it is an error if any excess arguments are provided.

let-optionals*

Binding constructs for optional procedure arguments. ARGS should be a rest-parameter taken from a lambda-list. let-optionals binds VAR1 ... to available arguments in parallel, or to DEFAULT1 ... if not enough arguments were provided. let-optionals* binds VAR1 ... sequentially, so every variable sees the previous ones. If a single variable RESTVAR is given, then it is bound to any remaining arguments, otherwise it is an error if any excess arguments are provided.

Other binding forms

and-let*

[syntax] (and-let* (BINDING ...) EXP1 EXP2 ...)

SRFI-2. Bind sequentially and execute body. BINDING can be a list of a variable and an expression, a list with a single expression, or a single variable. If the value of an expression bound to a variable is #f, the and-let* form evaluates to #f (and the subsequent bindings and the body are not executed). Otherwise the next binding is performed. If all bindings/expressions evaluate to a true result, the body is executed normally and the result of the last expression is the result of the and-let* form. See also the documentation for SRFI-2.

parameterize

[syntax] (parameterize ((PARAMETER1 X1) ...) BODY ...)

Binds the parameters PARAMETER1 ... dynamically to the values X1 ... during execution of BODY .... (see also: make-parameter in Parameters). Note that PARAMETER may be any expression that evaluates to a parameter procedure.

receive

SRFI-8. Syntactic sugar for call-with-values. Binds variables to the result values of VALUEEXP and evaluates BODY ....

The syntax

(receive VALUEEXP)

is equivalent to

(receive _ VALUEEXP _)

set!-values

[syntax] (set!-values (NAME ...) EXP)

Assigns the result values of expression EXP to multiple variables.

Substitution forms and macros

define-constant

[syntax] (define-constant NAME CONST)

Define a variable with a constant value, evaluated at compile-time. Any reference to such a constant should appear textually after its definition. This construct is equivalent to define when evaluated or interpreted. Constant definitions should only appear at toplevel. Note that constants are local to the current compilation unit and are not available outside of the source file in which they are defined. Names of constants still exist in the Scheme namespace and can be lexically shadowed. If the value is mutable, then the compiler is careful to preserve its identity. CONST may be any constant expression, and may also refer to constants defined via define-constant previously. This for should only be used at top-level.

define-inline

Defines an inline procedure. Any occurrence of NAME will be replaced by EXP or (lambda (VAR ... [. VAR]) BODY ...). This is similar to a macro, but variable-names and -scope will be correctly handled. Inline substitutions take place after macro-expansion. EXP should be a lambda-expression. Any reference to NAME should appear textually after its definition. Note that inline procedures are local to the current compilation unit and are not available outside of the source file in which they are defined. Names of inline procedures still exist in the Scheme namespace and can be lexically shadowed. This construct is equivalent to define when evaluated or interpreted. Inline definitions should only appear at toplevel.

define-for-syntax

Defines the toplevel variable NAME at macro-expansion time. This can be helpful when you want to define support procedures for use in macro-transformers, for example.

Conditional forms

select

[syntax] (select EXP ((KEY ...) EXP1 ...) ... [(else EXPn ...)])

This is similar to case, but the keys are evaluated.

unless

[syntax] (unless TEST EXP1 EXP2 ...)

Equivalent to:

(if(not TEST)(begin EXP1 EXP2 ...))

when

[syntax] (when TEST EXP1 EXP2 ...)

Equivalent to:

(if TEST (begin EXP1 EXP2 ...))

Record structures

define-record

[syntax] (define-record NAME SLOTNAME ...)

Defines a record type. Call make-NAME to create an instance of the structure (with one initialization-argument for each slot). (NAME? STRUCT) tests any object for being an instance of this structure. Slots are accessed via (NAME-SLOTNAME STRUCT) and updated using (NAME-SLOTNAME-set!STRUCTVALUE).

define-record-printer

Defines a printing method for record of the type NAME by associating a procedure with the record type. When a record of this type is written using display, write or print, then the procedure is called with two arguments: the record to be printed and an output-port.

Other forms

assert

[syntax] (assert EXP [STRING ARG ...])

Signals an error if EXP evaluates to false. An optional message STRING and arguments ARG ... may be supplied to give a more informative error-message. If compiled in unsafe mode (either by specifying the -unsafe compiler option or by declaring (unsafe)), then this expression expands to an unspecified value. The result is the value of EXP.

begin-for-syntax

[syntax] (begin-for-syntax EXP ...)

Equivalent to (begin EXP ...), but performs the evaluation of the expression during macro-expansion time.

cond-expand

[syntax] (cond-expand FEATURE-CLAUSE ...)

Expands by selecting feature clauses. This form is allowed to appear in non-toplevel expressions.

ensure

[syntax] (ensure PREDICATE EXP [ARGUMENTS ...])

Evaluates the expression EXP and applies the one-argument procedure PREDICATE to the result. If the predicate returns #f an error is signaled, otherwise the result of EXP is returned. If compiled in unsafe mode (either by specifying the -unsafe compiler option or by declaring (unsafe)), then this expression expands to an unspecified value. If specified, the optional ARGUMENTS are used as arguments to the invocation of the error-signalling code, as in (error ARGUMENTS ...). If no ARGUMENTS are given, a generic error message is displayed with the offending value and PREDICATE expression.

eval-when

[syntax] (eval-when (SITUATION ...) EXP ...)

Controls evaluation/compilation of subforms. SITUATION should be one of the symbols eval, compile or load. When encountered in the evaluator, and the situation specifier eval is not given, then this form is not evaluated and an unspecified value is returned. When encountered while compiling code, and the situation specifier compile is given, then this form is evaluated at compile-time. When encountered while compiling code, and the situation specifier load is not given, then this form is ignored and an expression resulting into an unspecified value is compiled instead.

The following table should make this clearer:

In compiled code

In interpreted code

eval

ignore

evaluate

compile

evaluate at compile time

ignore

load

compile as normal

ignore

include

[syntax] (include STRING)

Include toplevel-expressions from the given source file in the currently compiled/interpreted program. If the included file has the extension .scm, then it may be omitted. The file is searched in the current directory and, if not found, in all directories specified in the -include-path option.

nth-value

[syntax] (nth-value N EXP)

Returns the Nth value (counting from zero) of the values returned by expression EXP.

time

[syntax] (time EXP1 ...)

Evaluates EXP1 ... and prints elapsed time and some values about GC use, like time spent in major GCs, number of minor and major GCs.