Next: Examples Up: Core Units Previous: Examples

## Linking Units and Creating Compound Units

The `compound-unit` form links several units into one new compound unit. In the process, it matches imported variables in each sub-unit either with exported variables of other sub-units or with its own imported variables: ``

      (compound-unit
         (import variable   )
         (link [tag (sub-unit-expr linkage   )]   )
         (export (tag exportage   )   ))

      linkage is one of:
         variable
         (tag variable)
         (tag variable   )

      exportage is one of:
         variable
         (internal-variable external-variable)

      tag is:
         identifier

The three parts of a `compound-unit` expression have the following roles:

- The `import` clause imports variables into the compound unit. These imported variables are used as imports to the compound unit's sub-units.
- The `link` clause specifies how the compound unit is created from sub-units. A unique tag is associated with each sub-unit, which is specified using an arbitrary expression. Following the unit expression, each linkage specifies a variable using the variable form or the `(tag variable)` form. In the former case, the variable must occur in the `import` clause of the `compound-unit` expression; in the latter case, the tag must be defined in the same `compound-unit` expression. The `(tag variable )` form is a shorthand for multiple adjacent clauses of the second form with the same tag.
- The `export` clause re-exports variables from the compound unit that were originally exported from the sub-units. The tag part of each `export` sub-clause specifies the sub-unit from which the re-exported variable is drawn. The exportages specify the names of variables exported by the sub-unit to be re-exported.

  As in the `export` clause of the `unit` form, a re-exported variable can be renamed for external references using the `(internal-variable external-variable)` form. The internal-variable is used as the name exported by the sub-unit, and external-variable is the name visible outside the compound unit.

The evaluation of a `compound-unit` expression starts with the evaluation of the `link` clause's unit expressions (in sequence). For each sub-unit, the number of variables it imports must match the number of linkage specifications that are provided, and each linkage specification is matched to an imported variable by position. Each sub-unit must also export those variables that are specified by the `link` and `export` clauses. If, for any sub-unit, the number of imported variables does not agree with the number of linkages provided, the `exn:unit` exception is raised. If an expected exported variable is missing from a sub-unit for linking to another sub-unit, the `exn:unit` exception is raised. If an expected export variable is missing for re-export, the `exn:unit` exception is raised.

The invocation of a compound unit proceeds in two phases to invoke the sub-units. In the first phase, the compound unit resolves the imported variables of sub-units with the bindings provided for the compound unit's imports and new bindings created for sub-unit exports. In the second phase, the internal definitions and expressions of the sub-units are evaluated sequentially according to the order of the sub-units in the `link` clause. The result of invoking a compound unit is the result from the invocation of the last sub-unit.

Next: Examples Up: Core Units Previous: Examples