2.2.1 Writing modules

A definition module is a construct that provides access to a set of publicized definitions. They can appear anywhere, but are typically found in the outer reach and compiled separately, in which case they conform a prelude packet (see Packets). They are composed of a prelude and a postlude. The publicized definitions appear in the module’s prelude.

Consider for example the following definition module, which implements a very simple logging facility:

module Logger =
   def int fd = stderr;
       pub string originator;
       pub proc log = (string msg) void:
              fputs (fd, (originator /= "" | ": ") + msg + "\n");

       log ("beginning of log\n");
   postlude
       log ("end of log\n");
   fed

The module text delimited by def and fed gets ascribed to the module indicator Logger. Module indicators are bold tags. Once defined, the module Logger is accessible anywhere within its reach.

The prelude of the module spans from def to either postlude, or to fed in case of modules not featuring a postlude. It consists on a restricted serial clause in a void strong context, which can contain units and declarations, but no labels or completers. The declarations in the prelude may be either publicized or no publicized. As we shall see, publicized indicators are accessible within the reach of the defining module publicizing them. Publicized declarations are marked by preceding them with pub.

In our example the module prelude consists on three declarations and one unit. The indicator fd is not publicized and is to be used internally by the module. The indicators originator and log, on the other hand, are publicized and conform the interface of the module. Note how the range of the prelude also covers the postlude: the log procedure is reachable there, as it would be fd as well.

The postlude of the module is optional and spans from postlude to fed. It consists on a serial clause in a void strong context, where definitions, labels and module accesses are not allowed, just units.