Thoughts on Namespaces and OO

vkvalli 2006-03-24: Here are some of my thoughts on Tcl namespaces. Here I would like people to put their thoughts on Namespaces.

One of the major strengths of Tcl is having minimal concepts - commands and args and composing complex things out of this.

One easy way to judge a language's simplicity is to see the number of "special characters" in the language.

It seems the namespaces seems to fit into Tcl in an inconsistent way. To maintain consistencey, most of the things which are done using infix operators in other languages are done using prefix operators in Tcl. For exaple, set equivalent to the = operator. Do we really need a two different concept as Object and Namespace and can we blend those two? Both seems to deal with a way of creating isolated context for procs.

Larry Smith interjects:

Actually, we have a similar conceptual problem with Tk, where widgets are constructed using "." - .menu.submenu.function.edit. In all these cases we are actually dealing with a list - which Tcl has as a native data type, but we didn't use it, apparently because the original implementors didn't realize these were all congruent concepts.

Ideally, we'd use lists for all this stuff.

namespace { top-space local-space this-space } { code }
set { top menu submenu function edit } [ .button ... ]

...and so on. Notice that we have eliminated the need for the infix '::', thereby preserving Tcl's minimalism.

vkvalli continues:

Namespaces introduce a way of showing relationship using infix operator '::'. Is there a way we can have namespace with prefix operator- ?

Namespaces create a isolated context for procs and their common data. It seems to have more resemblance with class object. Can we integrate these two. So that we need one mechanism to create context, or object. Class objects, unlike instance objects, will have only one instance and can serve the purpose of namespace.

namespace import might become a problem -?. More thoughts are welcome.

George Peter Staplin 2006-03-24 PYK 2020-02-05: The prefix notation for namespaces is interesting. My primary complaint about namespaces in Tcl are that they can still conflict. We could have better namespaces than C++ or Java because they can be dynamically created. I believe that namespaces should be generated at the time of package require, using the prefix that the user of the code suggests, rather than the package designer's choice. There are various ways of getting around this, such as using

namespace eval $ns {code here}

, or the code at Local Packages and Transparent Namespaces.

Complaint 2. the implementation has undesirable side effects that I've been told can't be fixed until Tcl 9.0, such as

set var global_value
namespace eval ns { set var local_value}

which chooses to modify an existing variable in the global namespace rather than creating a new variable in the current namespace. There are also bugs in the tracker regarding namespaces and traces.

Complaint 3. The Tcl namespace structures and functions for creating and manipulating namespaces have public names (Tcl_), but they are not exported, so extensions must either use Tcl_Eval to create namespaces or or risk breakage by including tclInt.h and therefore not work with Tclkit.

tb 2007-08-29: I just imported Basique - OO-like namespaces into TclTalk and found it quite interesting. I tweaked it a little just to create classes in ::Classes like ::Classes::File and also to create the corresponding OO-like routine like ::Classes::File. I can see all basique classes showing up as children of ::Classes in the namespace tree and find their class procedures in the ::Classes namespace. I also tried to create a package from ::Classes::File, but the resulting package knew nothing about its class procedure in ::Classes. I'll go on playing with basique, as it would give namespaces a more OO-like cloaking. here is my patched version of basique 1.0.