Previous chapter: Variables

Dylan manual: Modules

5. Modules

See Also Dylan Design Notes: Module Defining Forms (Addition)

Modules are used for creating large-scale variable namespaces. Modules let you build programs and program segments that have private variables. Only the variables you export are visible from outside the module. Module Variables are similar to global variables, unit variables, and pool variables of other languages. module variables

Some languages have module systems with distinct support for exporting variables, functions, types, and classes. Dylan modules operate only on variables. Because functions and classes are stored in variables, you can control access to them by controlling access to the variables that contain them. If you export the variable containing a class or function, you have effectively exported the class or function. If you do not export the variable, then you have effectively kept the class or function private. FN11

Dylan modules are manipulated by the programming environment, and by nonportable run-time operations.

Overview of Modules

A module gets its variables from two sources. Variables can be created in the module, or they can be accessible through useing another module. Variables accessible through using another module are sometimes called imported variables.

The module system guarantees that there will never be an ambiguity between these two sources. If a conflict arises when a module tries to create a new variable, use a new module, or export a variable, the conflict is resolved or the operation is canceled.

All the variables in a module are either unexported or exported. Variables are initially unexported. To make a variable accessible to other modules, it must be exported. The exported variables make up the documented interface to the classes and functions of a module.

References to Module Variables

Dylan expressions are compiled in the context of a particular module. When an expression is executed, the module is used to look up all the variables that do not have lexical definitions. In the following example, the variables + and my-number would be looked up in the module in which the expression was compiled. The variable x is a lexical variable, so it is not looked up in any module. (bind is a special form, so it is treated specially by the compiler. The interactions between special forms and macros and the module system will be described in a future version of Dylan.)

(bind ((x 10))
  (+ x my-number))
The module to use when compiling an expression is determined by the programming environment.

Adding Variables to a Module

See Also: Dylan Design Notes: No Incremental Class Modifications (Change)

See Also: Dylan Design Notes: Result Type Declarations (Addition)

define[return to first citation]                      [Macro]
define-class[next citation]                [Macro]
define-generic-function[next citation]   [Macro]
define-method[next citation]             [Macro]
define-slot[next citation]                 [Macro]
These macros all add variables to a module, provided the variable does not already exist. They are described in detail in other sections of the manual.

These macros do not take an explicit module argument. They operate on the module in which they are compiled.

define-class and define-generic-function create read-only variables.

Using Modules

Using is the process whereby one module (the target module) imports the exported variables of another module (the source module). Once a variable is imported, it appears in the target module just like variables that were created in the target module.

In the simplest case, all the exported variables of the source module are imported into the target module. During normal program development, as variables are exported from the source module, they will be made available immediately in the target module.

The variables imported by the use operation can be restricted. A specified list of variables can be excluded (and all others will be imported), or a specified list of variables can be imported (with all others excluded).

Variables can also be renamed in the process of importing them. That is, a variable can have one name in the source module and another name in the target module. Renaming occurs on a per-symbol basis, not on a per-variable-name basis. That is, you can specify that the symbol foo should be remapped to the symbol bar; this specification would affect both a variable named foo as well as a variable named (setter foo). In addition to renaming individual symbols, you can specify that all symbols should be prepended with a given string in the target module.

Readonly Variables

Module variables may be read-only. A read-only variables cannot be assigned to a new value. The object contained in the variable can still be modified, but the variable cannot be modified to contain a different object.

Readonly variables are created by define-class and define-generic-function.

Next chapter: Introduction to Functions and Classes