Qore Programming Language Reference Manual  0.9.1
Namespaces
Description
Namespaces allow constants, classes, functions, global variables, and even other namespaces to co-exist in the same program by defining them in separate namespaces. Constants, classes, functions, and sub-namespaces can be declared to belong to a particular namespace either by defining them in-line within a namespace declaration, or by including the namespace name/path prepended to the constant, class, function, or namespace declaration separated by two colons "::". Global variables can only be declared out of line so they can be initialized when they are declared.

If the user does not specify the parent namespace with a namespace path in constant, class, function, global variable, or namespace declarations, the declaration will be by default in the unnamed default root namespace.

In-Line Namespace Declaration
[public] namespace [namespace_path::]namespace_identifier {
    [constant_declarations]
    [class_declarations]
    [function_declarations]
    [global_variable_declarations]
    [namespace_declarations]
}
Out of Line Namespace Declaration
[public] namespace namespace_identifier;
Note
No namespace path may be given with out of line namespace declarations.

When defining a user module, namespace declarations can be preceded by public, which means that the namespace's public contents will be exported into the Program object importing the module. When a namespace is declared public outside of a user module, it means that the namespace can be inherited in any child Program objects created in the same scope. See public for more information.

Namespace Resolution
Namespaces can either be resolved by giving a path to the constant, class, function, global variable, or namespace desired, or by leaving out the namespace path and allowing the system to search for the constant, class, function, global variable, or namespace. In either case, a depth-first search of the namespace tree is made for a match.

If a namespace path is included, then the tree is searched for the first namespace match, and, if the rest of the declaration cannot be matched, the search continues in the entire namespace tree until a complete match is found.

Namespace Paths
Namespace paths look like the following:
  • starting_namespace::[sub_namespace(s)s::]constant|class|namespace
Example
namespace MyNamespace {
# global variable declarations in a namespace declaration cannot be initialized at the point they are declared
our bool my_bool;
const MyConst = 1;
class MyClass {
}
namespace MySubNamespace;
}
# out of line initialization for MyNamespace::my_bool
MyNamespace::my_bool = False;
# out of line declaration and initialization for MyNamespace::my_int
our int MyNamespace::my_int = 200;
Note
No semicolon (";") should follow the closing bracket in a namespace declaration (in fact using a semicolon in this case would raise a parse exception).