Restore sanity to variable name resolution
Closed / Fixed959147ffffffffffffffffffffffffffffffffffCreated 23 years agoUpdated 19 days ago
Descriptionplain2004-05-23 23:28:31
There is a fundamental problem with varName resolution in namespaces. The variable lookup is as follows: (1) lookup in the current namespace; if the var exists, return a ref (2) lookup in the global namespace; if the var exists, return a ref (3) if the var has to be created, create it in the local namespace Quite apart from the difficulty of defining what it means for a variable to exist (see Bug #959052), this behaviour is surprising and dangerous. A simple variable call may refer to a ns-local or global variable, under circumstances that are difficult to check, and which may vary depending on non-local issues. namespace.n defines the rule as: "Command and variable names are always resolved by looking first in the current namespace, and then in the global namespace. Namespace names, on the other hand, are always resolved by looking in only the current namespace." That the process for variables is black magic is actually implicit in that man page: "You can use the namespace which command to clear up any question about name resolution." But there is no hint of the fragility of the procedure with respect to the existence of the global variable, especially for "creative writing" - ie, creating a variable and setting its value at the same time. Note that commands do not have this creative writing problem: [proc] always creates the command in the current namespace if the name is unqualified. This RFE proposes that we simplify the varName resolution rule in namespaces so that it is always simple and predictable, just as it is for varName resolution in proc bodies. The new description would be: "Command names are always resolved by looking first in the current namespace, and then in the global namespace. Namespace and variable names, on the other hand, are always resolved by looking in only the current namespace."
msofer Ticket [959147ffff]: 1 change
[History diff]2006-09-30 00:51:16
msoferplain2014-09-29 00:26:36
To illustrate the problem, consider:
% set a 0
0
% namespace eval x {set a 1; set b 1}
1
% set a
1
% set b
can't read "b": no such variable
% set x::a
can't read "x::a": no such variable
% set x::b
1
ferrieuxplain2014-09-29 07:31:15
+1 for this RFE, of course, as (unfortunately only a tiny) part of the Augean Stables lying ahead.
akuplain2014-09-29 15:07:13
IIRC this describes the "creative writing"-problem with var resolution, with the missing namespace var unexpectedly created in the global namespace. Yes, this should be fixed.
dkfwiki2016-07-14 07:11:29
This was even hitting namespace upvar until recently, but I changed that so that at least that command locates variables without resolvers involved. (That also technically touches the behaviour of global, but that was the case where the existing code mostly worked.)
serhiy.storchakawiki2026-11-13 11:18:45
Implemented by TIP 278 in Tcl 9.0: unqualified variable names in a namespace are resolved only in the current namespace, so the example above now sets x::a and leaves ::a alone.
Log in (anonymous is fine) to add a comment.