|
2010-01-19
| ||
| 16:57 | • Closed ticket [2929546fff]: "dict with" binds to varibles outside its ... artifact: aabd810e3a user: dkf | |
|
2010-01-13
| ||
| 03:17 | • Ticket [2929546fff]: 4 changes artifact: 2f38fdda1b user: mpdanielson | |
|
2010-01-12
| ||
| 16:05 | • Open ticket [2929546fff]. artifact: 170bf8eab0 user: dkf | |
| 16:05 | • Ticket [2929546fff]: 4 changes artifact: 464e269480 user: dkf | |
| 01:11 | • Closed ticket [2929546fff]. artifact: 0886a85ad4 user: mpdanielson | |
|
2010-01-11
| ||
| 23:10 | • Ticket [2929546fff]: 4 changes artifact: 5e18f4c188 user: johannes-kuhn | |
| 23:04 | • Ticket [2929546fff]: 4 changes artifact: 803b1e8f2c user: johannes-kuhn | |
| 00:54 | • New ticket [2929546fff]. artifact: dbd477c997 user: mpdanielson | |
"dict with" binds to varibles outside its scope
Closed / Fixed2929546fffffffffffffffffffffffffffffffffCreated 17 years agoUpdated 17 years ago
Descriptionplain2010-01-11 00:54:40
Setting a variable inside a "dict with" body changes the value of the dictionary and also changes the value of a variable with the same name outside of the body. In the following sequence, the value of ::a is changed when the value of the entry named "a" in the dictionary "x" is changed. I expected that only the dictionary would be changed.
(paul) 101 % set tcl_patchLevel
8.6b1.1
(paul) 102 % set a A
A
(paul) 103 % dict set x a 1
a 1
(paul) 104 % set a
A
(paul) 105 % set x
a 1
(paul) 106 % dict with x {
set a
}
1
(paul) 107 % dict with x {
set a 2
}
2
(paul) 108 % set a
2
(paul) 109 % set x
a 2
johannes-kuhnplain2010-01-11 23:04:43
dict with works as follows:
it sets for each key a variable with the name of the key and the value in the same scope, and safes a list of variables that exist at this point.
After execution of the body it writes all changed variables that correspond to a key and new variables back to the dict.
A side-effect of this is that all variables with a corresponding key are overwritten.
e.g
% set a A
% dict set x a 1
% dict with x {}
% set a
would return 1
This affects all Versions of Tcl 8.5 and Tcl 8.6 and is documented.
Other example:
set b(1) 2
dict set x b 3
dict with x {}
-> can't set "b": variable is array
Imho this isn't a bug. It's a feature.
Tip: use apply and upvar to avoid this.
johannes-kuhnplain2010-01-11 23:10:28
Ok, I was wrong at one point: new variables are not mapped back. (no snapshot of the current existing variables)
mpdanielsonplain2010-01-12 01:11:22
allow_comments - 1 The documentation says the command "executes the Tcl script in body", but it doesn't specify the scope in which the body gets executed. I'd assumed that it executed in a new scope, since silently creating/overwriting a bunch of variables that are not explicitly named in the command seems Perl'ish..., and it certainly complicates code maintenance. But, as pointed out, it is easy to avoid the effect.
dkfplain2010-01-12 16:05:20
I view this as at most a documentation bug. Reopening to remind myself to review the docs so that this will be clear in the future.
dkfplain2010-01-12 16:05:22
allow_comments - 0
mpdanielsonplain2010-01-13 03:17:34
I hit this issue when a dictionary being passed as a parameter had an additional key added, and the new key happened to be the same string as a local variable in a procedure that used "dict with" on the dictionary. Neither the person who wrote the proc nor the person who added the key understood this side effect (I really cannot consider it a feature), so the resulting corrupted variable bug was mysterious. Even using "apply", it's still dangerous to use "dict with" on a dictionary parameter unless your naming convention for keys and local variables ensure the two won't conflict. Using "dict update [dict get ...] ..." avoids the problem, so perhaps an example of that would highlight the issue.
dkfplain2010-01-19 16:57:49
allow_comments - 1 Improved the docs (both 8.5 and 8.6)
Log in (anonymous is fine) to add a comment.