Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 5de805bca68104f56038eb7c871974618f3c20c7
Ticket: 959147ffffffffffffffffffffffffffffffffff
Restore sanity to variable name resolution
User & Date: msofer on 2014-09-29 00:26:36
Changes

  1. closer changed to: "nobody"
  2. comment changed to:
    There is a fundamental problem with varName resolution
    in namespaces. The variable lookup is as follows: 
    (1) lookup in the current namespace; if the var exists,
    return a ref
    (2) lookup in the global namespace; if the var exists,
    return a ref
    (3) if the var has to be created, create it in the
    local namespace
    
    Quite apart from the difficulty of defining what it
    means for a variable to exist (see Bug #959052), this
    behaviour is surprising and dangerous. 
    
    A simple variable call may refer to a ns-local or
    global variable, under circumstances that are difficult
    to check, and which may vary depending on non-local issues.
    
    namespace.n defines the rule as: 
    "Command and variable names are always resolved by
    looking first in the current namespace, and then in the
    global namespace. Namespace names, on the other hand,
    are always resolved by looking in only the current
    namespace."
    
    That the process for variables is black magic is
    actually implicit in that man page: "You can use the
    namespace which command to clear up any question about
    name resolution." But there is no hint of the fragility
    of the procedure with respect to the existence of the
    global variable, especially for "creative writing" -
    ie, creating a variable and setting its value at the
    same time.
    
    Note that commands do not have this creative writing
    problem: [proc] always creates the command in the
    current namespace if the name is unqualified.
    
    This RFE proposes that we simplify the varName
    resolution rule in namespaces so that it is always
    simple and predictable, just as it is for varName
    resolution in proc bodies. The new description would be:
    
    "Command names are always resolved by looking first in
    the current namespace, and then in the global
    namespace. Namespace and variable names, on the other
    hand, are always resolved by looking in only the
    current namespace."
    
  3. icomment:
    To illustrate the problem, consider:
    
    % set a 0
    0
    % namespace eval x {set a 1; set b 1}
    1
    % set a
    1
    % set b
    can't read "b": no such variable
    % set x::a
    can't read "x::a": no such variable
    % set x::b
    1
    
  4. login: "msofer"
  5. mimetype: "text/plain"
  6. severity changed to: "Minor"