Previous section: Programs, module declarations, and expressions

Dylan reference manual -- Module declarations

Module declarations

Modules are defined by define module forms.

define module module-name 	[Definition]
  [ module-clauses ] 
end [ module ] [ module-name ]
where each module-clause is either a use clause, a create clause, or an export clause. (see below) This form defines the module with the given name. It describes which modules are used by the module being defined, which variables are imported from the used modules, and which variables are exported by the module being defined.

module-name has the same syntax as a variable name. Module names are scoped to a library. The namespace of module names is distinct from that of variables. No variable with the name module-name is created.

Used modules

There is one or more use-clause for each module used by the module being defined. Each use-clause has the form:

use module-name [ , module-use-options ] ;

module-name is the name of a module to be used. By default all exported variables from the used module are imported by the module being defined, under the same name they had in the used module.

When there are multiple use clauses using the same module, the set of imported variables is the union of those specified by all the use clauses. Some variables may be imported under more than one variable name.

Circular use relationships among modules are not allowed. The graph of the module-uses-module relation must be a directed acyclic graph.

The module-use-options are used to prevent some variables from being imported, to give some or all variables different names in the new module, and to re-export variables which were imported from a used module. Each of these options applies within the scope of the particular use clause, and does not affect the behavior of other use clauses (even if the other use clauses indicate the same module). The various options in a module-use-option may each appear no more than once.

import-option ::=

import: all

import: import-set

import-set ::=

{ importsopt }

imports ::=

import

import , imports

import ::=

variable-name

variable-name => variable-name

Indicates which variables are to be imported from the module being used. The default is all, meaning that all the variables exported by the used module should be imported. When => appears in an import-option, it specifies both an import and a rename. In other words, "import: {foo => bar}" is simply an abbreviation for "import: {foo}, rename: {foo => bar}" and means exactly the same thing.

exclude-option ::=

exclude: variable-name-set

variable-name-set ::=

{ variable-namesopt }

variable-names ::=

variable-name

variable-name , variable-names

Indicates variables which should not be imported. This keyword can only be used if import: is all. The default for the exclude: keyword is {}).

prefix-option ::=

prefix: string

Prepends string to the names of variables as they are imported from the used module. This option can be overridden for a particular variable by using the rename: keyword for that variable. The default value for the prefix: keyword is "" (the empty string).

rename-option ::=

rename: { rename-specsopt }

rename-specs ::=

rename-spec

rename-spec , rename-specs

rename-spec ::=

variable-name => variable-name

Used to override the import:, exclude:, and prefix: keywords. The variables named are imported, regardless of whether the import: and export:keywords indicate they should be imported. old-name indicates the name of the variable in the module being used; new-name indicates the name to be given to the variable in the module being defined. The prefix: keyword of the use clause is ignored for variables specified by the rename: keyword. The default value for the rename: keyword is {}.

export-option ::=

export: all

export: variable-name-set

Specifies variables which should be exported from the module being defined. Each of these variables must have been imported by this use clause. variable-name is the name of the variable in the module being defined. It is also the name under which the variable will be exported. It is allowed for the same variable-name to appear more than once, as this is sometimes useful for documentation purposes. all indicates that all the variables imported by this use clause should be exported. The default value for the export: keyword is {}.

Exporting owned variables

A module export clause has the following syntax:

export variable-names

This option specifies that the named variables are to be exported from the module being defined. Each variable-name is the name of a variable to export. These variables must be defined by a defining form in the module being defined. It is an error if any of the variables were imported from other modules. It is allowed for the same name to appear more than once, since this is sometimes useful for documentation purposes.

A module create clause has the following syntax:

create variable-names

This option specifies that the named variables are to be created in and exported from the module being defined. A variable-name is the name of a variable to create and export. These variables must not be defined by a defining form in the module being defined, and they must be defined by a module which uses the module being defined. It is an error if any of the variables were imported from other modules. It is allowed for the same name to appear more than once, since this is sometimes useful for documentation purposes.

Next section: Examples