Last update Mar 19, 2002
Module: ModuleDeclaration DeclDefs DeclDefs DeclDefs: DeclDef DeclDef DeclDefs DeclDef: AttributeSpecifier ImportDeclaration EnumDeclaration ClassDeclaration InterfaceDeclaration AggregateDeclaration Declaration Constructor Destructor Invariant Unittest StaticConstructor StaticDestructor DebugSpecification VersionSpecification ;Modules have a one-to-one correspondence with source files. The module name is the file name with the path and extension stripped off.
Modules automatically provide a namespace scope for their contents. Modules superficially resemble classes, but differ in that:
ModuleDeclaration: module ModuleName ; ModuleName: Identifier ModuleName . IdentifierThe Identifier preceding the rightmost are the packages that the module is in. The packages correspond to directory names in the source file path.
If present, the ModuleDeclaration appears syntactically first in the source file, and there can be only one per source file.
Example:
module c.stdio; // this is module stdio in the c packageBy convention, package and module names are all lower case. This is because those names have a one-to-one correspondence with the operating system's directory and file names, and many file systems are not case sensitive. All lower case package and module names will minimize problems moving projects between dissimilar file systems.
ImportDeclaration: import ModuleNameList ; ModuleNameList: ModuleName ModuleName , ModuleNameListThe rightmost Identifier becomes the module name. The top level scope in the module is merged with the current scope.
Example:
import c.stdio; // import module stdio from the c package import foo, bar; // import modules foo and bar
For example, assume the following modules:
Module foo int x = 1; int y = 2; Module bar int y = 3; int z = 4;then:
import foo; ... q = y; // sets q to foo.yand:
import foo; int y = 5; q = y; // local y overrides foo.yand:
import foo; import bar; q = y; // error: foo.y or bar.y?and:
import foo; import bar; q = bar.y; // q set to 3
Cycles (circular dependencies) in the import declarations are allowed as long as not both of the modules contain static constructors or static destructors. Violation of this rule will result in a runtime exception.