Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 9551919225e73c5e8b8c23a8d86c83a198ff56a2
Ticket: 1143805fffffffffffffffffffffffffffffffff
Impurity of empty [list]
User & Date: lars_h on 2005-02-18 13:59:28
Changes

  1. assignee changed to: "dkf"
  2. cmimetype changed to: "text/plain"
  3. comment changed to:
    A comment on the Tcler's Wiki page on [concat] (http://wiki.tcl.tk/
    1037) made me aware of the fact that the result of [list] is not 
    always a pure list; the result of [list] when called without 
    arguments is rather an empty string. This difference can be seen 
    on the script level as follows:
    
    % set third [expr 1.0/3]
    0.333333333333
    % expr {$third*3}
    1.0
    % expr {[lindex [concat [list $third] [list x]] 0]*3}
    1.0
    % expr {[lindex [concat [list $third] [list]] 0]*3}
    0.999999999999
    
    In the second case, the second argument of [concat] is not a pure 
    list, and thus [concat] operates on its arguments in string mode, 
    forcing a loss of the internal representation of the third.
    
    The bug is thus: The result of [list] is not always a pure list.
    
    Now, if anyone wants to argue that this is not a bug, then I can't 
    really object; since everything is (or at least should be) a string, 
    and in particular an empty list is the same thing as an empty 
    string, Tcl_ListObjCmd is completely right when it simply leaves 
    the empty result in the interpreter. Judging from the way it is 
    written, this is even by design:
    
    int
    Tcl_ListObjCmd(dummy, interp, objc, objv)
        ClientData dummy;                   /* Not used. */
        Tcl_Interp *interp;                 /* Current interpreter. */
        register int objc;                  /* Number of arguments. */
        register Tcl_Obj *CONST objv[];     /* The argument objects. */
    {
        /*
         * If there are no list elements, the result is an empty object.
         * Otherwise set the interpreter's result object to be a list 
    object.
         */
    
        if (objc > 1) {
            Tcl_SetObjResult(interp, Tcl_NewListObj((objc-1), 
    &(objv[1])));
        }
        return TCL_OK;
    }
    
    OTOH, the distinction between arbitrary lists and pure lists is 
    something that many programmers take care to observe, because 
    of the efficiency improvements this can bring, and then it seems a 
    bit odd that all return values from [list] _except_ the empty one 
    should be pure. This can lead to strange effects in cases like
    
       set extraOpts [list]
       foreach opt {-foo -bar -baz} {
          if {[needOption $opt]} then {lappend extraOpts $opt}
       }
       eval [list someCmd $someHugeDataStructure] $extraOpts
    
    where the [eval] suddenly takes much more time when no extra 
    options are passed, since $extraOpts will then not be a pure list.
    
    Also, I often find myself writing
    
       set res [list]
    
    rather than
    
       set res {}
    
    precisely because I know that this will be a list and want to "speed 
    things up" by initialising to an object with a list representation, and 
    I am not the only one who does this -- "set someVar [list]" is quite 
    common on the Wiki. It now turns out that this is in vain, since 
    what [list] returns actually doesn't have a list representation!
    
  4. foundin changed to: "obsolete: 8.4.9"
  5. priority changed to: "5 Medium"
  6. resolution changed to: "None"
  7. status changed to: "Open"
  8. submitter changed to: "lars_h"
  9. subsystem changed to: "17. Commands I-L"
  10. title changed to: "Impurity of empty [list]"
  11. type changed to: "Bug"