Namespaces
A namespace is a unique name that represents a group of related classes, subroutines, functions, and other (nested) namespaces. You can think of it as an abstract container for these members. Its primary purpose is to prevent naming collisions between classes.
Two namespaces are supplied with Synergy/DE:
- The System namespace
- The Synergex.SynergyDE namespace
All classes that are distributed with Synergy/DE are in one of these two namespaces. (See System-Supplied Classes for more information about these classes.)
Synergy/DE also has a global namespace called synglobal. Subroutines and functions in traditional Synergy that have no declared namespace are automatically added to the global namespace.
You can use the SYNDEFNS environment variable to specify the names of other namespaces that you want to import automatically.
Creating a namespace
1. | Add a namespace declaration to your source file using the NAMESPACE statement. (See NAMESPACE-ENDNAMESPACE.) |
2. | Add one or more class declarations within the namespace. (See Creating a class.) You can also add structures, functions, and subroutines to the namespace. |
A namespace can be partially defined in multiple files.
Importing a namespace
Importing enables you to use classes in a namespace defined in another file without having to fully qualify them. The System and Synergex.SynergyDE namespaces, as well as any namespaces defined with SYNDEFNS, are imported automatically to all files. Namespaces defined in the current file, even if they occur later in the file, are available as well. You can use the IMPORT statement to import other namespaces into specific files.
You can import namespaces from other source files in the compilation unit and from prototype (.dbp) files that are available to the compilation unit. To make a prototype file available, specify its location with the -qimpdir compiler option or the SYNIMPDIR environment variable. (The directories listed with -qimpdir are searched first.)
For example, say you have the namespace MyNamespace, which contains a class MyClass and a method MyMethod. You can use MyMethod in any file within the compilation unit (or any file that has access to the MyNamespace prototype) like this:
MyNamespace.MyClass.MyMethod()
However, if you import MyNamespace, you can then use MyMethod like this:
import MyNamespace . . . MyClass.MyMethod()
The scope of an IMPORT statement is limited to the file in which it occurs, so you must add an IMPORT statement to every file in which you wish to take advantage of the abbreviated syntax.
For more information, see