manju sharma
manju sharma

Reputation: 35

Namespace Structure in TCL

I have been reading about namespaces in TCL. I was trying to understand the structure of namespaces (using some sort of venn diagrams) in TCL but as i read more articles on the internet , they add to my confusion.

What i am certain about at this stage is that there is one global scope and , one local scope for procedures.

Can anyone throw some light on how namespaces are arranged in TCL or provide some good articles to read.

Thanks

Upvotes: 0

Views: 644

Answers (2)

Donal Fellows
Donal Fellows

Reputation: 137767

Namespaces are named collections in Tcl. They can effectively have three things in them: commands, variables and other namespaces (forming a tree). The things that they contain can all be looked up by name. Namespaced names use :: as a separator, much like / and/or \ are directory name separators (depending on your platform).

There is always a global namespace (usually called :: because the empty string is more awkward to work with) and recent versions of Tcl have a few others (notably ::tcl and ::oo). The global namespace is unique per interpreter; other interpreters have their own global namespace. There isn't anything that's a parent of the global namespace.

Namespaces have one other major trick: like a procedure stack frame, they can be pushed onto the stack. (Conceptually they're both instances of an abstract pushable thing, except we don't implement things that way.) Note that procedure stack frames are always associated with a current namespace which they use to look up the names of commands (and which the variable command looks up variables in). That current namespace is also where proc makes procedures and where the create method of classes makes instances, in both cases provided the name given is unqualified (or relative).

There are some additional complexities, but basically they're just the holders of all the main named entities in Tcl.

Upvotes: 1

Chris Heithoff
Chris Heithoff

Reputation: 1873

They are similar to absolute and relative filepaths on a computer disk. Think of a tree instead of a Venn diagram.

The :: is a namespace separator just like a / is a path separator in a Linux file system.

A namespace starting with :: is an absolute namespace, just like a file path starting with / is an absolute filepath.

A namespace not starting with :: is a namespace relative to the current namespace, just like a filepath not starting with / is a relative filepath.

The Tcl global namespace, ::, is at the top of the tree, just like / is at the top of a Linux file system.

Tcl namespaces can have child namespaces and will have a parent namespace (unless it's the global namespace).

Upvotes: 2

Related Questions