Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2026-10-21
10:03 • Closed ticket [fbc88fb645]: variable doesn't follow variable resolution ru... artifact: b157dd36fe user: serhiy.storchaka
2017-06-01
20:54 • Ticket [fbc88fb645]: 3 changes artifact: 91eace70f4 user: sebres
17:01 • Ticket [fbc88fb645]: 3 changes artifact: 85aa0e9d1e user: dgp
2017-05-31
16:09 • Ticket [fbc88fb645]: 3 changes artifact: 14386efd0b user: sebres
13:07 • Open ticket [fbc88fb645]. artifact: cdcd39a701 user: dgp
06:57 • Closed ticket [fbc88fb645]. artifact: 2190cee623 user: sebres
2017-05-30
20:56 • New ticket [fbc88fb645]. artifact: cffd6b1618 user: emiliano

[variable] doesn't follow variable resolution rules

Closed / Fixedfbc88fb645c5b2f47f16cd0e167ef47de081f212Created 10 years agoUpdated 43 days ago
Descriptionplain2017-05-30 20:56:02
The [variable] command, when used inside a procedure, doesn't follow the variable resolution rules as stated in namespace(n):
"Variable names are always resolved by looking first in the current namespace, and then in the global namespace"
When using relative names, it fails to look in the global namespace.
In this interactive session, from the global namespace: 

% namespace eval a {variable a "a in [namespace current]"}
% namespace eval foo {puts $a::a}
a in ::a
% namespace eval foo::a {variable a "a in [namespace current]"}
% namespace eval foo {puts $a::a}
a in ::foo::a
% namespace eval foo {proc bar {} {puts $a::a}}
% foo::bar
a in ::foo::a
% namespace delete foo::a
% foo::bar
a in ::a
% namespace eval foo {proc bar {} {variable a::a; puts $a}}
% foo::bar
can't access "a::a": parent namespace doesn't exist
% namespace eval foo::a {variable a "a in [namespace current]"}
% foo::bar
a in ::foo::a

the error shown is not expected; it should instead print "a in ::a", same as using $a::a directly inside the proc.
sebreswiki2017-05-31 06:57:35

You are wrong with your assessment: in your error-case the variable is not fully qualified (thus relative current namespace), so it references "::foo::a::a" which is indeed not available.

Either you want to reference "::foo::a::a" (then you should create this var):

% namespace eval foo {namespace eval a {set a "a in [namespace current]"}; proc bar {} {variable a::a; puts $a}}
% foo::bar
a in ::foo::a
Rather you want to reference "::a::a" (then you should write it full-qualified):
% namespace eval foo {proc bar {} {variable ::a::a; puts $a}}
% foo::bar
a in ::a
Thus closed as invalid.

dgpplain2017-05-31 13:07:20
I asked for this ticket to be opened.

If [variable] resolves varnames differently
from $-substitution, that needs to be understood
far more clearly.
sebreswiki2017-05-31 16:09:08

> If [variable] resolves varnames differently from $-substitution
I don't think so...

Following:


set ::a "::a (of global)";
namespace eval ::a {set a "[namespace current]::a"}
set cnt [namespace eval ::foo {
  namespace eval a {set a "[namespace current]::a"}
  set a "[namespace current]::a"
  proc init {} {
    set i 0
    foreach {vmap rmap} {
      a           ::foo::a
      a::a        ::foo::a::a
      ::a         ::a
      ::a::a      ::a::a
      ::foo::a    ::foo::a
      ::foo::a::a ::foo::a::a
    } {
      incr i
      set args [list [list i $i] [list vmap $vmap] [list rmap $rmap]]
      proc test$i $args {
        variable $vmap
        set vexp {"$a" eq "[set $rmap]"}
        puts -nonewline [format "%d: %15s is %-15s == " $i $vmap $rmap]
        puts [format " %5s == {%s eq %-15s} -- {%s}" [if $vexp {set _ OK} else {set _ WRONG}] {$a} $rmap [subst $vexp]]
      }
      puts "init: proc test$i { $args } ..."
    }
    return $i
  }
  init
}]
puts {}; set i 0; time {foo::test[incr i]} $cnt; set _ {}
results to the:

init: proc test1 { {i 1} {vmap a} {rmap ::foo::a} } ...
init: proc test2 { {i 2} {vmap a::a} {rmap ::foo::a::a} } ...
init: proc test3 { {i 3} {vmap ::a} {rmap ::a} } ...
init: proc test4 { {i 4} {vmap ::a::a} {rmap ::a::a} } ...
init: proc test5 { {i 5} {vmap ::foo::a} {rmap ::foo::a} } ...
init: proc test6 { {i 6} {vmap ::foo::a::a} {rmap ::foo::a::a} } ...
6
% puts {}; set i 0; time {foo::test[incr i]} $cnt; set _ {}

1: a is ::foo::a == OK == {$a eq ::foo::a } -- {"::foo::a" eq "::foo::a"} 2: a::a is ::foo::a::a == OK == {$a eq ::foo::a::a } -- {"::foo::a::a" eq "::foo::a::a"} 3: ::a is ::a == OK == {$a eq ::a } -- {"::a (of global)" eq "::a (of global)"} 4: ::a::a is ::a::a == OK == {$a eq ::a::a } -- {"::a::a" eq "::a::a"} 5: ::foo::a is ::foo::a == OK == {$a eq ::foo::a } -- {"::foo::a" eq "::foo::a"} 6: ::foo::a::a is ::foo::a::a == OK == {$a eq ::foo::a::a } -- {"::foo::a::a" eq "::foo::a::a"}

What I'm doing wrong, resp. which point I miss here?

Respectively which resolver exactly acts differently? I hold it for impossible (as long as not some own interp/namespace variable-resolver used).

dgpplain2017-06-01 17:01:15
Didn't even try to decipher that.

Look at the original ticket. First two commands
demonstrate that when the current namespace is ::foo,
the variable name "a::a" can resolve to ::a::a .  It
does so when ::foo::a::a does not exist and ::a::a
exists.

The 8th command demonstrates that the same resolution
happens within a proc body under the same conditions.

The 9th and 10th commands demonstrate that when the
[variable] command is brought into the picture within a
proc body where the current namespace is ::foo, and
when ::foo::a::a does not exist and ::a::a does exist,
when "a::a" is passed as an argument to [variable] it
does not get the same resolution treatment.  This is at
least surprising and sure smells like a bug.  At best it's
an ugly inconsistency.

Thus the title: [variable] doesn't follow variable resolution rules
sebreswiki2017-06-01 20:54:52

Aaaa... now I've got it. Then more understandable example should be provided:

% namespace eval a {variable a "a in [namespace current]"}
% catch {namespace delete ::foo}; \
  namespace eval foo { set i 0; time { incr i
    proc test1 {} {catch {set a::a} r; set r}
    puts -nonewline "${i}.a. direct:   "; puts [test1]
    proc test2 {} {catch {upvar a::a a; set a} r; set r}
    puts -nonewline "${i}.b. as upvar: "; puts [test2]
    proc test3 {} {catch {variable a::a; set a} r; set r}
    puts -nonewline "${i}.c. variable: "; puts [test3]
    # repeat with existsing ::foo::a
    if {$i == 1} { puts "Create ::foo::a now ..."
      namespace eval a {variable a "a in [namespace current]"}
    }
  } 2 }; set _ {}

1.a. direct: a in ::a 1.b. as upvar: a in ::a 1.c. variable: can't access "a::a": parent namespace doesn't exist Create ::foo::a now ... 2.a. direct: a in ::foo::a 2.b. as upvar: a in ::foo::a 2.c. variable: a in ::foo::a

Why test 1.c. fails? Resp. why resolving of var with "variable" acts differently, if the relative namespace does not exists?

Actually it is so documented. It works as designed, just because the completely resolving of var-name by "variable" will be always mapped relative current namespace, thus to the "::foo::a::a" (but ns "::foo::a" not exists, so BOOM). Emphasis to "always" - regardless of whether this variable/namespace exists or not exists.

More interesting question would be vice versa - why "upvar a::a a" resolves differently as "variable a::a"? And why it is documented differently...

serhiy.storchakawiki2026-10-21 10:03:36

Fixed in Tcl 9.0 by TIP #278 ([580ef6f047]): relative qualified variable names no longer fall back to the global namespace, so $a::a and variable a::a agree. 8.6 keeps the old rules for compatibility.

Log in (anonymous is fine) to add a comment.