Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Variable resolution behaviour and documentation

Closed / Invalid1251123fffffffffffffffffffffffffffffffffCreated 21 years agoUpdated 20 years ago
Descriptionplain2005-08-03 15:51:49
Consider the following: 

(bin) 1 % namespace eval ::N {set bar 2} 
2 
(bin) 2 % puts $bar 
can't read "bar": no such variable 
(bin) 3 % puts $::N::bar 
2 
(bin) 4 % set foo 1 
1 
(bin) 5 % puts $foo 
1 
(bin) 6 % namespace eval ::N {set foo 2} 
2 
(bin) 7 % puts $foo 
2 
(bin) 8 % puts $::N::foo 
can't read "::N::foo": no such variable 

I would expect line 6 to create ::N::foo, in the same way 
that line 1 created ::N::bar, but Tcl notices that ::foo 
exists, and somehow resolves to it. The NAME 
RESOLUTION section of the [namespace] manual page 
says that for unqualified names the following 
applies: "Command and variable names are always 
resolved by looking first in the current namespace, and 
then in the global namespace". This applies 'correctly' 
to variable *access*, but in a funny way to variable 
*creation*. 

What is worse, command creation behaves differently: 
invoking [proc] with an unqualified procedure name 
within a namespace creates the procedure in that 
namespace irrespectively on whether or not an equally 
named procedure already exists in the global 
namespace. 

I understand that modyifing variable resolution rules is 
something to be avoided. What I propose is adding a 
sentence to the NAME RESOLUTION section of the 
[namespace] manual page above describing this 
observed behaviour.
msoferplain2005-08-03 23:20:06
Logged In: YES 
user_id=148712

I agree that this behaviour is nasty. It probably will have
to remain as it is in the Tcl8 line, and be fixed in Tcl9.

The problem is compounded with a surprising definition of
"existence" for variables (see bug 959052).

The fix is of course to avoid accessing ns variables without
"declaring" them first using the [variable] command. This is
very frustrating.

Do you have a concrete proposal for that explanatory sentence?
ooxplain2005-08-03 23:59:26
Logged In: YES 
user_id=662619

I've seen bug 959052: whow!

>
> Do you have a concrete proposal for 
> that explanatory sentence? 
>
I knew you were going to ask that one!  :-)

Given the fact that [variable] "behaves properly", I concede 
that writing such a sentence is difficult.

In fact the current behaviour for variables is consistent with 
the documentation: if the variable does not exist in the 
current namespace, Tcl scans the global namespace: if it 
does exist there, then Tcl resolves to that. The problem, as I 
see it, is that [proc] behaves differently:

% proc x {} {puts "Proc in [namespace current]"}
% namespace eval ::N { proc x {} {puts "Proc in [namespace 
current]"} }

That creates both ::x and ::N::x !

May we add something like "(except that [proc] will create a 
new procedure in a namespace instead of overwiting an 
equally named procedure in the global namespace)"  ?

Juan Carlos---
dkfplain2005-08-04 00:15:25
Logged In: YES 
user_id=79902

Don't mix command and variable resolution.  Command
resolution is right.
msofer Closed ticket [1251123fff]. [History diff]2006-09-30 08:11:24
Log in (anonymous is fine) to add a comment.