Modules



next up previous contents index
Next: Stand-Alone Routine Implementations Up: Implementations Previous: Implementations

Modules

The form of a module is:

        <module> -> module [<implements>] [<impl_elt>]* end
        <impl_elt> -> <routine_def> | <class_def> | <maker_def> | <equate>
Every module must contain at least one class or routine implementation. The implements clause identifies the routine specifications that are being implemented by what module and defines which routine implementations in the module implement each specification:
        <implements> -> implements <exported_item> ["," <exported_item>]*
        <exported_item> -> <idn> ["{" <idn_list> "}"] | <idn> "[" <parms> "]" "{" <idn_list> "}"

The first kind of exported_item identifies a routine_interface (9.1) that is being implemented within the module. The optional idn_list names the routine_defs that implement that specification; the list can be omitted if there is a single implementation with the same name as that of the routine interface that is implemented. The second kind of exported_item indicates that the module implements a specific instantiation of a parameterized routine_interface (9.3); in this case an idn_list naming one or more routine_defs must be provided. The module must contain all the routine_defs indicated in the implements clause, and the type of a routine_def must be a subtype of the type of the routine_interface or instantiation that it implements. The module can contain other routine_defs, but these can be used only within the module (unless a class provides one of these routines to its subclasses 10.5.1).

For example, a module that implements the "bag" type defined in Section 9.4 might say:

module implements create_bag
to indicate that it is exporting an implementation of the "create_bag" routine, and furthermore the routine_def within the module is also named "create_bag". Users can call this routine (after instantiating it) to obtain bags that are implemented with whatever class is indicated in that "create_bag" implementation. Alternatively, a module might provide a specialized implementation for "bag[char]", e.g.,
module implements create_bag[char]{cbag}
indicating that internal routine "cbag" will implement the instantiation "create_bag[char]". Users can make use of "cbag" to create a new "bag[char]" with the specialized implementation.

Using code does not use the routine names exported by modules. Instead the code uses names introduced in routine specifications. Before the code runs, a linker binds every use of a routine specification to a routine that implements that specification. For example, in the following code:

        b: bag[char] := create_bag[char]( )
create_bag[char] could be linked to "cbag".

A module defines a scope (4). Modules do not nest: no module is ever contained within another module.

Modules provide encapsulation. Details of a class are visible throughout its module so that, for example, a routine in the module can access the instance variables of objects of the class. The internal details of a class are never visible to code outside its module.

[usage1249]



next up previous contents index
Next: Stand-Alone Routine Implementations Up: Implementations Previous: Implementations



theta-questions@lcs.mit.edu