Artifacts Associated With Ticket 2929546
Ticket change [dbd477c997] (rid 108027) by mpdanielson on 2010-01-11 00:54:40:
- assignee initialized to: "dkf"
- cmimetype initialized to: "text/plain"
- comment initialized to:
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 - foundin initialized to: "obsolete: 8.6b1.1"
- is_private initialized to: "0"
- priority initialized to: "5 Medium"
- resolution initialized to: "None"
- status initialized to: "Open"
- submitter initialized to: "mpdanielson"
- subsystem initialized to: "15. Dict Object"
- title initialized to: ""dict with" binds to varibles outside its scope"
- type initialized to: "Bug"
Ticket change [803b1e8f2c] (rid 97703) by johannes-kuhn on 2010-01-11 23:04:43:
- icomment:
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. - login: "johannes-kuhn"
- mimetype: "text/plain"
- username: "johannes-kuhn"
- icomment:
Ticket change [5e18f4c188] (rid 115580) by johannes-kuhn on 2010-01-11 23:10:28:
- icomment:
Ok, I was wrong at one point: new variables are not mapped back. (no snapshot of the current existing variables)
- login: "johannes-kuhn"
- mimetype: "text/plain"
- username: "johannes-kuhn"
- icomment:
Ticket change [0886a85ad4] (rid 89574) by mpdanielson on 2010-01-12 01:11:22:
- closedate changed to: "2010-01-11T18:11:22"
- closer changed to: "mpdanielson"
- icomment:
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.
- login: "mpdanielson"
- mimetype: "text/plain"
- resolution changed to: "Invalid"
- status changed to: "Closed"
- username: "mpdanielson"
Ticket change [464e269480] (rid 99496) by dkf on 2010-01-12 16:05:20:
- icomment:
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.
- login: "dkf"
- mimetype: "text/plain"
- username: "dkf"
- icomment:
Ticket change [170bf8eab0] (rid 124812) by dkf on 2010-01-12 16:05:22:
- icomment:
allow_comments - 0
- login: "dkf"
- mimetype: "text/plain"
- status changed to: "Open"
- username: "dkf"
- icomment:
Ticket change [2f38fdda1b] (rid 93026) by mpdanielson on 2010-01-13 03:17:34:
- icomment:
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.
- login: "mpdanielson"
- mimetype: "text/plain"
- username: "mpdanielson"
- icomment:
Ticket change [aabd810e3a] (rid 100977) by dkf on 2010-01-19 16:57:49:
- closedate changed to: "2010-01-19T09:57:49"
- closer changed to: "dkf"
- icomment:
allow_comments - 1 Improved the docs (both 8.5 and 8.6)
- login: "dkf"
- mimetype: "text/plain"
- resolution changed to: "Fixed"
- status changed to: "Closed"
- username: "dkf"