An Introduction to the Tingle Programming Language

Dirk van Deun, dirk at

Section 7: Name spaces

The language as presented up to here does not scale to projects that use libraries or frameworks or in any way reuse code written by someone else. We still need to introduce a mechanism by which any reusable body of code can be used through a small interface, without exposing all its internals. A set of classes, which export only their top methods, could serve as such an API; but the ideas out of which they consist should be hidden. Simple name spaces are enough to arrange this. We can put a library in a separate name space and refer to its classes using fully qualified names:

w = new Widget@WidgetLibrary

Alternatively, we can import the class Widget into the current name space using syntax that we already know:

class Widget = Widget@WidgetLibrary.

w = new Widget

In both cases, we can use a Widget without worrying whether some of the ideas that make up the class Widget have the same names as some of our own ideas: these ideas are still in another name space. (You can actually refer to ideas by fully qualified names too, at your own risk: name spaces hide ideas and classes, but do not make them inaccessible to the determined hacker. The point is rather that there are no or very few good reasons to do this.)

We are not limited to using library classes as they are: we can also extend them with our own mixins:

namespace main.

as myWidget {
class MyWidget = Widget@WidgetLibrary myWidget.

The class MyWidget consists of all the ideas of Widget plus our own idea myWidget. If one of the ideas out of which Widget is composed were also named myWidget, it would still be separate from our own myWidget, as it lives in the same class but in a different name space.

What we cannot do, is nest myWidget inside the ideas that make up Widget, as we are not supposed to know about them. So myWidget is necessarily a mixin. The next section will present a new way to use nesting, which alleviates this problem without throwing away the benefits of name spaces. First two final remarks about name spaces.

One, name spaces are easy to use correctly: pieces of code that were not designed together should always be in separate name spaces. All class and idea names in a Tingle program are implicitly qualified with the current name space, except if a name space is explicitly specified; so within the default name space "main", any reference to an idea named myWidget will be interpreted as myWidget@main.

Two, you might wonder whether name spaces might be equivalent to an extra idea around all the code that is in them. They are not, because they do not exist in isolation. They have no content of their own and you have no choice whether to include them in a class or not. Instead, name spaces can be thought of as simply a mechanism for rewriting idea and class names, adding suffixes like "@WidgetLibrary".

Contents | Section 8: Your Class is my Idea