On this page:
module
#%module-begin
#%plain-module-begin
Version: 4.1

2.1 Modules: module, ...

The module Form in Guide: PLT Scheme introduces module.

(module id module-path form ...)

Declares a module. If the current-module-declare-name parameter is set, the parameter value is used for the module name, otherwise (quote id) is the name of the declared module.

The module-path must be as for require, and it supplies the initial bindings for the body forms. That is, it is treated like a (require module-path) prefix before the forms, except that the bindings introduced by module-path can be shadowed by definitions and requires in the module body forms.

If a single form is provided, then it is partially expanded in a module-begin context. If the expansion leads to #%plain-module-begin, then the body of the #%plain-module-begin is the body of the module. If partial expansion leads to any other primitive form, then the form is wrapped with #%module-begin using the lexical context of the module body; this identifier must be bound by the initial module-path import, and its expansion must produce a #%plain-module-begin to supply the module body. Finally, if multiple forms are provided, they are wrapped with #%module-begin, as in the case where a single form does not expand to #%plain-module-begin.

After such wrapping, if any, and before any expansion, an 'enclosing-module-name property is attached to the #%module-begin syntax object (see Syntax Object Properties); the property’s value is a symbol corresponding to id.

Each form is partially expanded (see Partial Expansion) in a module context. Further action depends on the shape of the form:

After all forms have been partially expanded this way, then the remaining expression forms (including those on the right-hand side of a definition) are expanded in an expression context.

The scope of all imported identifiers covers the entire module body, as does the scope of any identifier defined within the module body. The ordering of syntax definitions does not affect the scope of the syntax names; a transformer for A can produce expressions containing B, while the transformer for B produces expressions containing A, regardless of the order of declarations for A and B. However, a syntactic form that produces syntax definitions must be defined before it is used.

No identifier can be imported or defined more than once at any phase level. Every exported identifier must be imported or defined. No expression can refer to a top-level variable.

The evaluation of a module form does not evaluate the expressions in the body of the module. Evaluation merely declares a module, whose full name depends both on id and (current-module-name-prefix).

The module body is executed only when the module is explicitly instantiated via require or dynamic-require. On invocation, expressions and definitions are evaluated in order as they appear within the module; accessing a module-level variable before it is defined signals a run-time error, just like accessing an undefined global variable.

See also Modules and Module-Level Variables and Module Phases and Visits.

(#%module-begin form ...)

Legal only in a module begin context, and handled by the module form.

The pre-defined #%module-begin form wraps every top-level expression to print non-#<void> results using current-print.

(#%plain-module-begin form ...)

Legal only in a module begin context, and handled by the module form.