[dict for] "value" var name escapes code block scope
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
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
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.