Next Previous Contents

10. Name Spaces

By default, all global variables and functions are defined in the global namespace. In addition to the global namespace, every compilation unit (e.g., a file containing S-Lang code) has an anonymous namespace. Objects may be defined in the anonymous namespace via the static declaration keyword. For example,

     static variable x;
     static define hello () { message ("hello"); }
defines a variable x and a function hello in the anonymous namespace. This is useful when one wants to define functions and variables that are only to be used within the file, or more precisely the compilation unit, that defines them.

The implements function may be used to give the anonymous namespace a name to allow access to its objects from outside the compilation unit that defines them. For example,

     implements ("foo");
     static variable x;
allows the variable x to be accessed via foo->x, e.g.,
     if (foo->x == 1) foo->x = 2;

The implements function does more than simply giving the anonymous namespace a name. It also changes the default variable and function declaration mode from public to static. That is,

     implements ("foo");
     variable x;
and
     implements ("foo");
     static variable x;
are equivalent. Then to create a public object within the namespace, one must explicitly use the public keyword.

Finally, the private keyword may be used to create an object that is truly private within the compilation unit. For example,

    implements ("foo");
    variable x;
    private variable y;
allows x to be accessed from outside the namespace via foo->x, however y cannot be accessed.


Next Previous Contents