Namespaces

Definition

Variables are distributed in a hierarchical namespace system.
By default, and without any other notice, every variables are set in the global namespace.

i = 1;
str = "abc";

Namespace separator character is the backslash ('\').
A variable from the global namespace can be referenced by prepending a backslash to its name.

\i++; // 2
\str .= "def"; // abcdef

A namespace is created by setting a variable in the namespace.

\app\counter = 4;
\app\counter++; // 5

Namespaces of any depth can be created at any time.

\apps\trantor\meta\version = 0.1;

Usage

Definition

The code defined in a function or a class considers the container variable's namespace as its local namespace.

\app\counter = 0;
\app\incrementCounter = function() {
    counter++; // search for a "counter" variable inside the "\app" namespace
               // (first in the function's scope, and then in the global scope)
};
\app\incrementCounter(); // \app\counter == 1
\trantor\users = 0;
\trantor\engine = class {
    addUser = function() {
        \trantor\users++;
    };
};
\trantor\engine.addUser(); // \trantor\users == 1

Scope modification

The counterpart of the previous statement is that it is possible to change the scope of a function's code by copying the variable.

\aaa\i = 0;
\bbb\i = 8;
\aaa\f = function() {
    i++;
};
\aaa\f(); // \aaa\i == 1
\bbb\f = \aaa\f;
\bbb\f(); // \bbb\i == 9

This behaviour should not be a problem:

  • Objects should stand data encapsulation.
  • Namespaces can always be used in their "full path" form.