Note

You are not reading the most recent version of this documentation. 1.4.7 is the latest version available.

Structure of a Coco Program

Each source file creates a module in Coco. A Coco program may consist of several modules.

Modules

A module defines a namespace and groups together a number of declarations within it.

A module can refer to definitions in another module, in which case one module depends on the other. However, there must not be any cyclic dependencies between modules. Including modules with cyclic dependencies is a static error.

Declarations

A module may contain the following kinds of declaration:

All declarations are lexically scoped: they are visible with the context in which they occur and any of its nested declarations.

Namespaces and Imports

Each module has its own namespace, so a single name may be used in several different modules, with a distinct meaning in each. In order to use a name declared in another module, the module must be imported.

declarationImport
import [unqualified] «module name» [as «new module name»]

In its simplest form, import X, all names declared in X may then be used as X.«name». It is not possible to import only some of the names in a module.

As in Java, the module M must be stored in the file M.coco. A dot (.) in the module name will be converted into a directory separator, so import M.N looks for the file M/N.coco.

Modules can also be imported with a different name using the as keyword followed by a new module name. For example:

import X as Y

In this case, names declared in the imported module X may be used as Y.«name».

Names declared in the current module may not be used qualified (prefixed with the name of the current module).

Alternatively, modules may be imported unqualified, using the keyword unqualified. For example:

import unqualified X

In this case, names declared in the imported module X may be used unqualified in the current module.

It is a static error to use an unqualified name in the current module if it is ambiguous; i.e. if it could refer to two different declarations. An unqualified import of a module that contains declarations of names that are also used in the current module is likely to lead to ambiguous references and thus static errors.

Note

The use of unqualified imports is generally not recommended. However, the standard library is imported unqualified by default into every module.