[   ^ to index...   |   <-- previous   |   next -->   ]

What are modules for?

The idea behind a module system, regardless of the language, is twofold:

ML modules have many sophisticated features to support these goals. However, when used in the simple manner described on the previous page, structures allow us to keep separate namespaces for related definitions. This reduces the possibility that names will conflict as our system grows. However, namespacing is merely one of the simplest mechanisms whereby parts of a system can be isolated from each other.

When you wish to avoid typing the structure names, you can use the open directive:

open MyStuff;
val whale = myAdd(MyInt(5), MyReal(5.0));

Rampant opening of structures is discouraged. In fact, you should avoid using an open directive at the top level whenever possible, because it defeats the point. You can locally open in any smaller scope (for example, a let declaration or a structure), for more controlled results:

fun ocean() =
    let
        open MyStuff; (* open within this let declaration only *)
        val whale = myAdd(MyInt(5), MyReal(5.0));
    in
        myAdd(whale, MyInt(100))
    end;
val ocean = fn : unit -> MyStuff.MyNum

val aZero = myZero; (* remains illegal *)
/homes/gws/klee/341/section/04-19/code.sml:46.13-46.19 
   Error: unbound variable or constructor: myZero

Modules in C++

Since I've said that the goals of a module system are independent of language, you might be moved to ask: what is the module system in C++?

The answer: for the most part, C++ merges the notions of "module" and "data type" [0]. The class construct in C++ defines a type, and also defines the interface to that type. Classes are units of both scoping (class members are automatically visible within the class) and access control ("public", "protected", "private"). In ML, these roles are more cleanly separated.

[0] OK, this is not 100% true. C++ provides a "namespace" construct; also, the system of dividing a project into multiple header/body files is a kind of rough, ad hoc module system, one that is manifestly inferior to ML's.


Keunwoo Lee
Last modified: Thu Apr 19 08:02:24 PDT 2001