Namespaces act as containers for type declarations and implementations. Each Freya source file consists in a sequence of namespace declarations.
There's no entity in the Common Language Runtime (CLR) that directly matchs with a namespace definition. The main effect of namespaces is providing a full qualified path for the types declared inside their scopes. As a consequence you can declare types as if they belong to an already existing namespace, and a namespace declaration can be repeated both inside the same project and even the same file:
namespace System; // Though System is used by predefined types, // there's no problem defining new types in that namespace. namespace Freya.Demos // ... namespace Freya.Demos // Repeating a namespace declaration is not a problem. end.
A namespace is composed by an optional using clause with local scope, and several namespace sections. Allowed section types are:
Section | Purpose |
---|---|
public | Contains declarations for public types. |
internal | Contains declarations for internal types that cannot be used from outside the assembly. |
private | private and internal are considered synonyms at this level. |
implementation | Contains member implementations for a given class or record. |
This example shows a typical namespace declaration, including the global using clause from the file, and a local using clause:
using System, System.Text; namespace Freya.Examples using System.Collections.Generic; public MyEnum = ( { ... } ); MyClass = class // ... end; MyRecord = record // ... end; implementation for MyClass is // ... implementation for MyRecord is // ... end.
There are several points to note:
Though it's not a recommended practice, you can declare types in the global scope, without any enclosing namespace:
// Not recommended. using System; MyProgram = class // ... etc ... end; end.
This can be useful for quick test applications, but the cost is the pollution of the global scope.
The Freya Programming Language
Program and file structure
Lexical structure
Projects and source files
Implementation sections
using clauses
The entry point
Type declarations