Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2024-03-18
10:02 • Ticket [7cea8bfad0] dict for "value" var name escapes code block sco... artifact: d381cb8857 user: sethrader
10:00 • Closed ticket [7cea8bfad0]. artifact: 276a9829cc user: sethrader
09:57 • Ticket [7cea8bfad0]: 3 changes artifact: 3417ff3d93 user: sethrader
04:47 • Ticket [7cea8bfad0]: 3 changes artifact: 15d77ab401 user: apnadkarni
01:51 • Ticket [7cea8bfad0]: 3 changes artifact: 86214e5628 user: sethrader
2024-03-17
17:31 • New ticket [7cea8bfad0]. artifact: e2d2d51362 user: sethrader

[dict for] "value" var name escapes code block scope

Closed / Invalid7cea8bfad06c3f18421511d24e25cfa8a7d1fd87Created 3 years agoUpdated 3 years ago
Descriptionmarkdown2024-03-17 17:31:11

Running the following code will produce an error on attempting to declare a variable "h":

#!/usr/bin/env tclsh                                                                
                                                                                    
proc p {} {                                                                         
    variable d {a 0}                                                                
                                                                                    
    dict for {key h} $d {}                                                          
                                                                                    
    variable h "Should not be set already"                                          
}                                                                                   
                                                                                    
p 

Note: this only seems to occur in the body of a procedure. The code inside proc "p" will execute fine if executed outside a procedure body.

Platform: Linux/Tcl 8.6.14

Could not replicate the same issue on Tcl 8.6.13 under Wine

sethrader Ticket [7cea8bfad0]: 3 changes [History diff]2024-03-18 01:51:32
apnadkarnimarkdown2024-03-18 04:47:37

I don't understand the bug. Seems to me expected behavior. Why would you expect h to not be set? The dict for executes in the same scope as the containing code and introduces variables in that scope.

With respect to difference in behavior between proc and global cases, that has nothing to do with the dict command.

% set foo 1
1
% variable foo
% proc xx {} {set x 1; variable x}
% xx
variable "x" already exists
% namespace eval ns {set z 1; variable z}
%

Note only the proc case generates an error, not the global or namespace cases, because the command has different semantics inside a proc. At the global or namespace level, the command creates a variable. Within a proc, variable creates a link to the variable of that name in the containing namespace. Linked variables are not allowed if a variable of that name already exists. You can see this with upvar as well.

% set v 1
1
% proc yy {} {set x 1; upvar v x}
% yy
variable "x" already exists

I see the same behavior with Tcl 8.6.13 on Windows as 8.6.14. As to why you do not see the error with 8.6.13 on Wine, no idea.

% proc p {} {
    variable d {a 0}
    dict for {key h} $d {}
    variable h "Should not be set already"
}
% p
variable "h" already exists
% package require Tcl
8.6.13
sethradermarkdown2024-03-18 09:57:47

I apologize, I had simplified the actual code where the discrepancy was taking place in order to have a refined example, and mistook what I thought was a bug as the cause of the discrepancy. After simplifying the code, I neglected to check it again in 8.6.13. Now that I have reviewed the code, I realize that the discrepancy is not due to version differences in Tcl but differences in the environment I was executing in. Specifically, a configuration file was being read under wine that set the initial dictionary (d) to an empty dictionary, so h would never have been set.

Again, sorry for the inconvenience. Please close this ticket.

sethrader Closed ticket [7cea8bfad0]. [History diff]2024-03-18 10:00:50
sethrader Ticket [7cea8bfad0]: 4 changes [History diff]2024-03-18 10:02:00
Log in (anonymous is fine) to add a comment.