ivan73
ivan73

Reputation: 705

Tcl namespace definition

I can't find answer what is the difference in namespace definition using double :: ( when I read source files where both are used ) between:

namespace eval somenamespace {
}

and

namespace eval ::somenamespace {

}

sample without :: https://github.com/tcltk/tcllib/blob/master/modules/generator/generator.tcl#L16

sample with :: https://github.com/tcltk/tcllib/blob/master/modules/ftp/ftp.tcl#L56

Upvotes: 2

Views: 212

Answers (2)

Peter Lewerin
Peter Lewerin

Reputation: 13252

It's a bit like path names. If you are in the root directory (the unnamed / path) it makes no difference if you use bar or /bar: both refer to the /bar directory. If you are in /foo, it matters very much if you use bar or /bar: the first refers to the /foo/bar directory, and the second still refers to the /bar directory.

:: is like / for namespace names. In the root namespace (the empty :: name) it makes no difference if you use bar or ::bar: both refer to the ::bar namespace. If you are in ::foo, it matters very much if you use bar or ::bar: the first refers to the ::foo::bar namespace, and the second still refers to the ::bar namespace.

Documentation: namespace

Upvotes: 1

Donal Fellows
Donal Fellows

Reputation: 137557

In general, it depends on the context in which the code is run. If it is run in the global namespace, there is no difference between the two. If it is run inside another namespace (e.g., in ::foo for the sake of argument) there's a difference (since one creates ::foo::somenamespace).

For packages it makes little difference, the scripts provided by package ifneeded — and hence run by package require — are actually run by this line (inside tclPkg.c, in the function PkgRequireCore):

        code = Tcl_EvalEx(interp, script, -1, TCL_EVAL_GLOBAL);

That is, they're always in the global context, the :: namespace.

Upvotes: 1

Related Questions