Skip to content
Tcl Source Code View Ticket
Login

View Ticket

[variable] should always create a link

Closed / Rejected604226ffffffffffffffffffffffffffffffffffCreated 24 years agoUpdated 20 years ago
Descriptionplain2002-09-03 22:31:37
% namespace eval a variable x
% namespace eval b variable ::a::x
% set a::x 1
1
% info exists b::x
0
dgpplain2002-09-04 06:03:54
Logged In: YES 
user_id=80530


If this is not a bug, it's definitely a place to
clarify the docs.  Related behavior:

% namespace eval a variable x 1
% namespace eval b variable ::a::x
% puts $b::x
can't read "b::x": no such variable
% namespace eval b {
    variable ::a::x
    puts $x
}
can't read "x": no such variable
% proc b::foo {} {
    variable ::a::x
    puts $x
}
% b::foo
1

Different behavior of [variable] within
a proc compared to within a [namespace eval]
needs either correction or more explicit documentation.
msoferplain2004-03-28 07:20:57
Logged In: YES 
user_id=148712

Don: care to re-read variable.n? That is actually the
documented behaviour, except that the docs are not too
explicit about what happens with varnames with :: within
procs ...
The problem is: you (and I, and maybe everybody else ...)
expect 'variable' to create a local linked variable even
when not in a proc. But that is not what is promised, ie: it
is not true that
   variable ::a::x
is equivalent to
   upvar 0 ::a::x x
Apparently 'variable' was born to correct for the weird
lookup rules we have? 
So: what do we do? RFE/TIP? Leave as is?
dgpplain2004-03-29 23:10:49
Logged In: YES 
user_id=80530


I tend to think this is a mis-feature.
I accept I may be missing something
important, yet subtle.  I'd like to see
a TIP propsing the addition of setting
up namespace -> namespace variable
links by [variable] when not within a
proc.

If in fact, that is a bad idea, then some
of the examples in this report could
be incorporated into variable.n to
more explicitly demonstrate the difference.
dgp Ticket [604226ffff]: 1 change [History diff]2004-03-29 23:11:21
msofer Ticket [604226ffff]: 3 changes [History diff]2004-03-29 23:29:20
msoferplain2004-03-29 23:29:21
Logged In: YES 
user_id=148712

Reclassifying then as RFE. The request is that, whenever
[variable] addresses a namespace variable in a different
namespace, it also creates a local link. In code, that
whenever $name is a qualified variable name,
   variable $name $val
be equivalent to
   set lname [namespace tail $name]
   upvar 0 $name $lname
   set $lname $val

This would require a TIP.
dkf Ticket [604226ffff]: 2 changes [History diff]2004-03-30 15:30:44
msofer Ticket [604226ffff]: 1 change [History diff]2006-09-30 00:46:41
msoferplain2006-10-03 05:56:53
Logged In: YES 
user_id=148712

Uh ... not nice, [variable] is not even really a noop:

% namespace eval a variable x
% namespace eval b variable ::a::x 1
% set a::x
1
% set b::x
can't read "b::x": no such variable
msoferplain2006-10-31 02:36:43
Logged In: YES 
user_id=148712

We isunderstood the purpose and/or docs of [variable].
Looking at namespace-old.test, I see that the usage there
(and, from the comments, possibly an idiom in itcl) really
requires the current behaviour outside of proc bodies.

Consider test namespace-old.6.16 - to reduce headaches,
notice that
  - there are no linked variables there
  - $trigger has been last defined in test 6.12, and is
       {set test_ns_cache_var}
  - $trigger2 has been last defined in test 6.15 and is
       {set test_ns_cache2::test_ns_cache_var}

The test body is

 variable test_ns_cache1::test_ns_cache2::test_ns_cache_var
"blah"
 list [namespace eval test_ns_cache1 $trigger2] \
      [namespace eval test_ns_cache1::test_ns_cache2 $trigger]

Here [variable] is being used to set the value of the variable

   ::test_ns_cache1::test_ns_cache2::test_ns_cache_var

from the global space, but without a fully qualified name
(why?). If a variable ::test_ns_cache_var were created to
point to it, all manner of nastyness could ensue: a new
GLOBAL variable that may break whatever code relies on the
second lookup path, and wasted resources as the programmer
never expected that new variable to be created.

In order to link variables, use [upvar] or [namespace upvar].
sf-robotplain2006-11-14 10:20:11
Logged In: YES 
user_id=1312539

This Tracker item was closed automatically by the system. It was
previously set to a Pending status, and the original submitter
did not respond within 14 days (the time period specified by
the administrator of this Tracker).
Log in (anonymous is fine) to add a comment.