Next: , Previous: , Up: Modules   [Contents][Index]


5.12.11 Module Definition

Formally, module definition needs an “interface”, a “config” and the body. An interface declares which bindings are exported, and a config is either usually an open or access clause.

Macro: define-structure name interface config body…
Macro: structure interface config body…

These special forms each create a new module with interface interface and configuration config. The full syntax of an interface is described in the next section. structure defines an anonymous structure.

After configuring the module as specified, the sequence of forms body… is evaluated; it should include the definitions required by the interface that the module has promised to implement.

The config form is either a list of configuration clauses, or a single configuration clause. Each such clause must be of the following syntax:

clause -> (open name …)
       |  (access name …)

Each name specifies the name of a module.

Always make it rule to open rep structure. It’s because Librep used to offer Scheme mimicry by importing scheme structure.

As described in the section Module and File, it only makes sense if a module is defined in a file with an appropriate path.

Here is an example module definition, defining a module named test that exports two functions foo and bar.

(define-structure test (export foo bar)
    (open rep)
  (define (foo x) (* x 42))
  (define (bar x y) (+ (foo x) (1+ y))))

It is also possible to export multiple views of a single underlying set of bindings, by using the define-structures form to create a number of modules.

Macro: define-structures ((name interface) …) config body…

Create a module for each (name interface) pair. The module is called name and exports the interface defined by interface.

The config and body… forms are as in define-structure.

Here is a trivial example:

(define-structures ((foo (export foo both))
                    (bar (export bar both)))
    (open rep)
  (define both 1)
  (define foo 2)
  (define bar 3))

the underlying environment has three bindings. Each created module exports two of these.


Next: , Previous: , Up: Modules   [Contents][Index]