- append - array set - binary scan - catch - dict append/create/others probably - file stat/file lstat - foreach - gets - incr - info default - lappend - lassign - proc - regexp - regsub - scan - set
% set a "global"
global
% namespace eval foo {set a "namespace"; set b "namespace"}
namespace
% set a
namespace
% set foo::a
can't read "foo::a": no such variable
% set b
can't read "b": no such variable
% set foo::b
namespaceHemang Lavana commented: To be safe, you should always use the `variable' command first to declare a variable in the current namespace.RS: But still, this requirement for a declaration is not in the spirit of Tcl.Proc definitions inside a namespace remain in that namespace, however; they don't go out to the global namespace, as Ronnie further showed: % proc a {} {puts global}
% namespace eval foo {proc a {} {puts namespace};proc b {} {puts namespace}}
% a
global
% foo::a
namespace
% b
ambiguous command name "b": binary break
% foo::b
namespaceRS: I think namespace resolution should distinguish between locating existing things (procs or variables) and determining an absolute name for non-existing things.- For locating existing things, the current resolution rule can be left unchanged.
- For "creative writing", creating new things, it should confine itself to the current namespace (as it currently does with procs). He who wants something done in the global namespace, can explicitly prepend :: to the name ;-)
set a 12
namespace eval foo { set a [expr {$a+1}] }Here we apparently have the same variable referenced twice, but what should be the effect of the second statement? There are a number of possibilities:- ::a (global) gets set to 13, ::foo::a doesn't exist (current behaviour of set);
- ::a is left as 12, ::foo::a gets set to 13 (call this a variable copy-on-write behaviour. Behaviour of the variable command);
- error "no such variable 'a' while executing 'expr {$a + 1}'" (lookup in current namespace only);
Update on 2007-01-16: Ralf Fassel pointed out another "creative writer" in comp.lang.tcl:
checkbutton .foo.bar.bazwill create/use a global variable 'baz' which might have surprising effects if 'baz' is already in use as global variable with a different meaning elsewhere in the program.LV I wonder if you investigate Tk, whether you will find more of these.RFox I think radiobutton does similar stuff.DKF: The rule is "always set the -variable for a checkbutton or radiobutton".AK: So why not make this option required ? (Is that actually possible in Tk ?)DKF: You've just answered why. There is no mechanism in the current option management code to make any option required during object creation (which is the only time you'd ever want it to be mandatory anyway).
TIP#278 [1] would take away the namespace problems with respect to creative writing, but apparently not the checkbutton problem.
