Namespace (keyword)
From RemObjects Software
Namespace replaces the unit keyword in the source file header:
namespace ConsoleApplication1; interface type Class2 = public class private protected public end; implementation end.
Namespaces spanning multiple files
Multiple source files can share the same "namespace" header, and items defined within them will be merged into the same namespace.
Explicit namespace declarations
Class names can contain explicit namespace names to override the namespace specified in the source file's namespace header.
For example:
namespace RemObjects.Namespace1; ... type Object1 = class(Object) end; RemObjects.Namespace2.Object1 = class(Object) method Method1; end; implementation method RemObjects.Namespace2.Object1.Method1; begin ... end;
would define two classes, RemObjects.Namespace1.Object1 and RemObjects.Namespace2.Object1.
Circular References
Oxygene fully supports circular references. This happens when both files use the other one in its interface section:
namespace File1; interface uses File2 type TClass1 = class private C1: Class2; end; ... implementation end. namespace File2; interface uses File1 type Class2 = class(Class1) private end; ... implementation end.
The above example doesn't compile in regular Pascal and the classes would have to be put in a single file to make it work. Generally Pascal compilers are single pass, referring to processing the file once before creating the output. Oxygene, however, uses multiple passes over the interface section.When we talk about a multi-pass compiler, we generally think about a slow c++ compiler, but multi-pass doesn't have to be slow. Oxygene doesn't use header files like c++ does, instead it uses fast readable .Net assemblies as libraries.
When compiling, Oxygene first reads the interface sections of all files, then goes over them as needed to resolve all types. This makes it possible to spread a namespace over multiple files. Because types are resolved after they are read, there are no dependency errors between the different files. All files are compiled at once.Also it's possible to put a single class in each file without having any circular reference problems between the files.
Nameless Namespaces
You can use the following at the top of a unit:
namespace; interface
Without a namespace identifier, the types defined in the unit will be namespace-less. This means that types are globally visible everywhere in the assembly or, if public, also from code that references the assembly.
It is not recommended that you do this and the code was introduced into Oxygene mainly to support the Avalon Codedom.
Notes
When we set out to design Oxygene, one of our goals was to provide proper namespace support for the Pascal language, namespace support that would seamlessly integrate into the way the CLR handles namespaces and would be familiar and intuitive for user familiar with C#. Also, it was important to us that Oxygene would allow the creation of libraries that could intuitively be used by both Oxygene users and users of other languages such as C#, Visual Basic etc.
The following 6 major points show the design decisions we made for Oxygene in order to make its namespace support as intuitive as possible:
- In Oxygene, what you see is what you get. If you type "namespace x.y.z", that's your namespace. Its important to realize that namespace replaces the 'unit' of classical Pascal.
- You can use the same namespace name in as many file headers as you'd like and - whether in the same project or when consuming the .dll from another Oxygene/C#/whatever project - these files are treated as one big namespace; all the classes can see and reference each other; circular references are not an issue.
- Even between different namespaces, circular uses clauses are not an issue. So you can have "uses b;" in one or more files of "namespace a;" and "uses a;" in one or more files of "namespace b;" without any problems. This finally allows you to spread class hierarchies conveniently across several files even of the classes need to reference each other.
- Regardless of the namespace that's set for a given file using the "namespace" keyword, you can always declare classes into any other arbitrary namespace by using the full name in the class declaration and method implementations, allowing you to have classes from a single file go into separate namespaces (a rare requirement, granted, but it does come in handy at times).
- All available types (i.e. types from all referenced assemblies and all types defined in the current project) are available at ANY place where you could use a type name, via their full name - regardless of whether their namespace is in the uses clause or not; you only need to "use" the namespace if you want to refer to the types via the short name (ie as "x := new MyObject(...);" without the namespace prefix.
- Finally, as a convenience feature for using large nested namespace hierarchies, Oxygene allows the use of the * wildcard in its uses clause, to use an entire namespace hierarchy. As a result, "uses System.Xml.*;", for example, gives access to the entire set of Xml classes - including those located in sub-namespaces such as System.Xml.XPath.
See Also
Product: RemObjects Oxygene (formerly known as Chrome)
Current version: 3.0
Previous Versions: 'Joyride' (2.0), 'Floorshow' (1.5), 'Adrenochrome' (1.0)
Glossary — Keywords — Language Features — Platform Features — Samples — Articles — How To — Issues
Categories: Text | Oxygene | Language | Keyword
