A definition is a type, function, global variable, class field, or static local variable.
Compilation Units and Packages
All definitions belong to a compilation unit and a package. A compilation unit is equivalent to a source file, and a package is a group of related compilation units. Packages can contain other packages, forming a hierarchical tree structure.
The first declaration in every compilation unit must specify the name of the package it belongs to.
package id1 dot id2 .. idn;
package id1 dot id2 .. idn [: packageVersion];
where dot is a full stop or period and packageVersion is an optional dot-separated list of numbers defining the current version of the package.
Examples:
package cm.lang;
package cm.lang : 2.1.1;
Visibility and Access
Exporting Definitions
All definitions contain an export declaration which determines where the definition is visible and accessible. There are three export modifiers: public, package and private.
- Public definitions can be read and written from anywhere.
- Private definitions can only be accessed by other definitions belonging to the same compilation unit (source file).
- Package definitions can be read and written by code belonging to the same package.
The fully qualified name of a definition is the name of its package followed by a dot and the name of the definition. It can be used to refer to definitions in other packages. Definitions in the same package or compilation unit can refer to each other using their unqualified names. Definitions from other packages must either be referred to using their fully qualified names or be imported into the current package or compilation unit first.
Fields and global variables declared as private can also have the public readable or package readable properties. The effect is the same as if their export declaration had been public or package, but only for reading. Writing remains restricted.
The public readable property can also be used with the package export modifier.
Examples:
private int globalInt = 24 : package readable;
public class AnimateCamera3D { private bool animationEnable : public readable; }
Importing Definitions
The use statement imports all public definitions from a package into the current compilation unit, or into the current package when found in a file named package.cm. The definitions can then be referred to by their unqualified names, provided there are no name clashes.
use path;
use path: path1, path2, .., pathn
where path is a dot-separated list of identifiers corresponding to a subset of a valid package name.
Examples:
use cm.runtime, cm.syntax;
use cm: basic, io, win, geometry.advanced;
Indirect Import
The imported definitions can also be exported again to packages using the current package with the help of the export keyword. The effect is the same as if the other packages had contained hidden use statements.
Example:
In this example, importing cm.doc has the effect of also importing cm.io:
package cm.doc;
use export cm.io;
Comments
0 comments
Please sign in to leave a comment.