Skip to content

SpecificationVIII · Programs and modules

8.2

Modules and projects

When a program grows, it is split into files: every file is a module, and a module uses what another one makes public by importing it.

Markdown version

8.2.1 How it works

A project groups packages. Every package has a name and a root folder, and every file inside that root is a module. The module's name is its path inside the root with dots instead of slashes.

Figure 8.2-1.A package with two modules. principal imports formas.geometria and uses its public subprogram.

A package called tienda with the module principal, which is the entry and holds the algorithm, and the module formas.geometria, with the public subroutine AreaCirculo and the private Ayudante. An import arrow goes from principal to geometria.

A package can be:

  • an application: it declares one or more entries, modules with an algorithm; one is chosen when running;
  • a library: it has no entries or algorithms; it only offers units to other packages.

Only the entry module contains an algorithm. The others contain subprograms, classes and the remaining units.

Failures in the project configuration —an entry that does not exist, a module outside every root, a cycle between packages— are not code diagnostics: they are project errors, with a stable code such as entryNotFound or packageCycle and the affected path.

8.2.2 Visibility

Every top-level unit is private to its module unless it carries the Public prefix. Only what is public can be imported.

Programformas/geometria.psl

Public Subroutine AreaCirculo(radio As Real) As Real
  Return 3.14159 * radio * radio;
EndSubroutine

Subroutine Ayudante()
EndSubroutine

This program is not run.

8.2.3 Importing

Import names a module or a symbol by its path, starting with the package name. An imported module is used through its last segment, or through the alias given by As:

Programprincipal.psl

Import tienda.formas.geometria;

Algorithm Principal
  Write geometria.AreaCirculo(2.0);
EndAlgorithm

This program is not run.

With Export, a module offers a public symbol of another module as its own, without creating a local name. Importing and exporting run nothing: an unused import does not change the result.

In short

  • Every file is a module; modules form packages and packages, a project.
  • Only what is marked Public can be imported.
  • Import uses paths that start with the package; As gives an alias.
  • Import cycles are not allowed.