| Ticket UUID: | fbc88fb645c5b2f47f16cd0e167ef47de081f212 | ||
| Title: | [variable] doesn't follow variable resolution rules | ||
| Type: | Bug | Created on: | 2017-05-30 20:56:02 |
| Submitter: | emiliano | Assigned to: | nobody |
| Subsystem: | 07. Variables | Severity: | Important |
| Priority: | 5 Medium | Last modified: | 2026-10-21 10:03:36 |
| Status: | Closed | Closed by: | nobody |
| Resolution: | Fixed | Closed on: | 2017-05-31 06:57:35 |
| Version: | 8.7 | ||
| Description: | ||||
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.
| ||||
| User Comments: | ||||
sebres added on 2017-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):
dgp added on 2017-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. sebres added on 2017-05-31 16:09:08:
> If [variable] resolves varnames differently from $-substitutionI don't think so... Following:
results to the:
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). dgp added on 2017-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 sebres added on 2017-06-01 20:54:52:
Aaaa... now I've got it.
Then more understandable example should be provided: 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.storchaka added on 2026-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. | ||||
