Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 15d77ab401910bcfc4da7e33f87ff56ae8e6d4cb70b71b1e22a943c0ba6e33da
Ticket: 7cea8bfad06c3f18421511d24e25cfa8a7d1fd87
[dict for] "value" var name escapes code block scope
User & Date: apnadkarni on 2024-03-18 04:47:37
Changes

  1. icomment:
    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
    ```
    
  2. login: "apnadkarni"
  3. mimetype: "text/x-markdown"