Declarations
Namespaces
Group declarations under a name, reopen a namespace to add to it, nest namespaces, and reach members with ::.
since 0.1.0-alpha.1linuxwindowswasm
Description
A namespace groups functions, classes, structs, enums, constants and variables under one name:
namespace Name {
...declarations...
}
Namespaces are declared in source, not by the layout of files on disk. The same namespace may be declared as many times as you like, in one file or several; every block adds to the same namespace. Namespaces nest, and a nested namespace is reached through its full path.
A member is reached from outside with ::: Geo::twice(), Geo::Shapes::sides(). Inside the
namespace its members are visible by their bare names. To use a namespace's names without writing
the path, import them with uses or use (see lang.imports).
The namespaces a package declares are visible in all of that package's files. A namespace from
anywhere else, including every namespace of the standard library, is a library: a file imports it
before using it, even by its qualified path (use io; makes io::console usable, and
use io::console; makes console usable bare).
Reopened and nested namespaces
use io::console;
namespace Geo {
int count = 0;
namespace Shapes {
int sides() => 4;
}
int bump() { count += 1; return count; }
}
namespace Geo {
int twice() => bump() + bump();
}
console.writeln(Geo::Shapes::sides());
console.writeln(Geo::twice());
Geo::count = 10;
Geo::count += 1;
console.writeln(Geo::count);
4
3
11
The second namespace Geo block adds twice to the namespace declared by the first block, and
twice can call bump by its bare name because both blocks are the same namespace.
Rules
- A namespace may be declared more than once; all blocks with the same path merge into one namespace.
- A namespace cannot share its name with a class, struct, interface, enum or attribute declared in the
same scope; put class-level operations in the class as
staticmembers (lang.static-members). - A namespace may contain other namespaces to any depth. Its members are named with a
::path. - A non-
constnamespace variable may be written by its qualified path from anywhere: from outside the namespace, from inside it, from a free function, from an instance method, or from a lambda. Plain assignment, compound assignment (+=) and indexed assignment (table[key] = v) are all allowed, at any depth (A::B::x = v). - The same variable may be written by its bare name wherever the bare name is in scope: inside the
namespace, or in a scope that imported it with
useoruses. Inside a class body, a bare name that the class also declares as a field is the class's own field first. - Writing a
constnamespace variable is a compile error, whether the write is bare, qualified, through an import alias, compound, or indexed. - A variable is identified by its full namespace path, not by its bare name:
namespace A { int k = 8; }andnamespace B { int k = 16; }are two independent variables, and a top-levelkis a third. A barekresolves by the ordinary scope rules (insidenamespace Ait isA::k), and an import names the variable of the declaration it imports. - A class is identified by its full path in the same way. A namespace may declare a class with the
name of a built-in type other than a primitive, such as
Geo::Array,Geo::MaporGeo::Range. It is an ordinary class:Geo::Array()constructs it,x is Geo::Arraytests for it, and aforloop over it uses its owniterator()(seelang.iterator-protocol). Inside that namespace a bareArraynames the namespace's class, and array literals anda..branges are still the built-in types, so one is a type error where the namespace's class is expected. Every built-in feature uses the standard library's own types, whatever a namespace declares:forover an array or a range,throwandcatch,using,await. - A top-level type may not take a built-in type's name. A program's top-level class, struct,
interface, enum,
use ... asalias or namespace named like one of the standard library's top-level types (Array,Range,Promise,IException, ...) is a compile error: the name is the core language's (lang.imports); declare it inside a namespace of another name. A primitive type's name (int,string,bool, ...) names no type, type parameter or namespace in any scope (lang.primitives). - Only a variable can be assigned. Assigning to a function name by any spelling (qualified
NS::f = ...orA::B::f = ..., bare inside its namespace, throughuses, or through ause ... asalias; plain, compound or indexed) is the compile errorcannot assign to function 'f', naming the name as written. Assigning to a namespace name (Foo::In = ..., or a bareIn = ...insideFoo) is the compile errorcannot assign to namespace 'In'. A variable of function type is an ordinary variable and can be assigned by every spelling (NS::fnVar = (int x) => x;), as can a local or a class field that shares a function's name. - Inside a class body, a bare assignment to a name that the class declares only as a method, when
no namespace variable of that name is visible, is the compile error
cannot assign to method 'g'. - Namespace-level variables are created with their default values before any initializer runs and
are initialized before the program's top-level statements. See
lang.constfor the full order.
Examples
A namespace variable can be updated from a method of a class that is not part of the namespace:
Writing a namespace variable from a method
use io::console;
namespace Cfg {
int hits = 0;
Array<int> log = [];
namespace Inner { int depth = 1; }
}
class Tracker {
void hit() {
Cfg::hits += 1;
Cfg::log.add(Cfg::hits);
}
}
Tracker().hit();
Tracker().hit();
Cfg::Inner::depth = 4;
console.writeln(Cfg::hits);
console.writeln(Cfg::log);
console.writeln(Cfg::Inner::depth);
2
[1, 2]
4
Writing a const variable or a function is rejected at compile time:
Assigning to a const and to a function
namespace Cfg {
const int limit = 5;
int size() => 1;
}
Cfg::limit = 6; // error: cannot assign to const 'limit'
Cfg::size = 3; // error: cannot assign to function 'size'
See also
- Imports — uses and use — Every library, the standard library included, reaches a file only through an import:
usefor one name or a namespace,usesfor all of a namespace's names.