Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2009-05-01
08:21 • Ticket [1274918fff] doc: upvar and qualified varNames status still Open with... artifact: 6ca5fcdc57 user: msofer
2006-09-30
08:09 • Ticket [1274918fff]: 1 change artifact: a1fb5c3cb4 user: msofer
2006-08-26
09:26 • Ticket [1274918fff]: 4 changes artifact: 25604fc518 user: msofer
02:45 • Ticket [1274918fff]: 4 changes artifact: 4bd9a39c78 user: msofer
2005-08-28
01:36 • New ticket [1274918fff]. artifact: 724f6d3095 user: msofer

doc: upvar and qualified varNames

Open1274918fffffffffffffffffffffffffffffffffCreated 21 years agoUpdated 18 years ago
Descriptionplain2005-08-28 01:36:47
This supersedes Bug #959831.

There are two (related, important) issues that the
[upvar] docs do not describe: what it does when called
outside a proc body, and how it handles qualified
variable names.

The behaviour outside of proc bodies just needs
documenting: 
   upvar ?level? otherVar myVar ?otherVar myVar ...?
will create a variable named myVar in the current
namespace (assuming for now that myVar is not
qualified) linked to the variable otherVar as resolved
in the corresponding stack frame. This is the same
behaviour as documented in upvar.c, just replacing
"current procedure" by "current context". The fact that
this variable can never be unset until the namespace is
deleted deserves mention - and maybe a fix too?

The behaviour when otherVar is qualified is completely
unspecified in the manual page. This may not be a
problem, the behaviour is without surprises: the
variable that is linked to is otherVar as resolved in
the corresponding stack frame.

The behaviour when myVar is qualified is also
unspecified in the manual page, and is currently buggy
(Bug #1274916).
msoferplain2006-08-26 02:45:32
Logged In: YES 
user_id=148712

Note that the issue for qualified otherVar is not quite trivial:
% namespace eval a {}; namespace eval a::b {}; namespace
eval b {}
% set b::x ::b
::b
% namespace eval a {upvar 0 b::x w; set w}
::b
% set a::b::x ::a::b
::a::b
% namespace eval a {upvar 0 b::x w; set w}
::a::b

In the example above
  namespace eval a {upvar 0 b::x w}
the new variable should(?) be linked (in order) to:

  (1) ::a::b::x if the namespace ::a::b exists
  (2) ::b::x     if the namespace ::b     exists

Note that this refers to the existence of namespaces; the
usual resolution rules (and the actual upvar behaviour)
refer to the existence of the variables themselves, the
reference is to:

  (1) ::a::b::x if the variable ::a::b::x exists
  (2) ::b::x     if the variable ::b::x     exists
  (3) ::a::b::x otherwise (even if ::b exists but ::a::b
does not)
msoferplain2006-08-26 09:26:37
Logged In: YES 
user_id=148712

There is madness in here:
% namespace eval a {namespace eval b {}}
% namespace eval b {}
% set b::x 1
1
% namespace eval a {
   upvar 0 b::x s b::y t
   set s 2
   set t 3
}
3
% set b::x
2
% info vars b::*
::b::x
% info vars a::b::*
::a::b::y

Ie: [upvar 0 b::x s b::y t] creates vars linked to different
namespaces. This is definitely not the path of least surprise.
msofer Ticket [1274918fff]: 1 change [History diff]2006-09-30 08:09:52
msofer Ticket [1274918fff]: 1 change [History diff]2009-05-01 08:21:25
Log in (anonymous is fine) to add a comment.