Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: a6eeb13f71318e5a39edee84c722ad90cda3dc04
Ticket: 217782ffffffffffffffffffffffffffffffffff
for loop with body "puts $i" does not compile correctly
User & Date: nobody on 2000-10-26 00:43:34
Changes

  1. assignee changed to: "nobody"
  2. cmimetype changed to: "text/plain"
  3. comment changed to:
    OriginalBugID: 727 Bug
    Version: 8.0.3
    SubmitDate: '1998-10-13'
    LastModified: '1999-08-13'
    Severity: SER
    Status: Closed
    Submitter: admin
    ChangedBy: hobbs
    RelatedBugIDs: 1669
    OS: All
    OSVersion: NA
    Machine: NA
    FixedDate: '2000-10-25'
    FixedInVersion: NA
    ClosedDate: '1999-08-13'
    
    
    "Jeffrey P. Talberg" wrote:
    > 
    > I was trying to create a simple tcl example that I can use for teaching
    > a course illustrating the difference between "" and {} grouping by using
    > the for command .
    [snip]
    > #-----------------------------------------
    > # This is the one I had a question about
    > #-----------------------------------------
    > for {set i 0} {$i < 10} {incr i} "puts $i"
    > 
    > Why does the last version still print the numbers 0-9?  I expected it to
    > print 0 10 times.  I thought the statement "puts $i" would become "puts
    > 0" when the for command underwent grouping and substitution prior to
    > command dispatch.
    
    You are correct, that's what it should do.
    
    > 
    > I was intrigued by these results, can anyone explain why tcl behaves
    > this way?
    
    That sure looks like a bug to me. Probably our friend the byte compiler
    doing some "optimizations" for us. 
    
    This has prompted me, finally, to reinstall tcl 7.6. It seems like there
    are lots of little bugaboos popping up that seem to be related to the
    byte compiler. Sure enough, running this example with 7.6 gives the
    expected results:
    
        % for {set i 0} {$i < 10} {incr i} "puts $i"
        can't read "i": no such variable
        % set i 0
        0
        % for {set i 0} {$i < 10} {incr i} "puts $i"
        0
        0
        0
        ...
    
    FWIW, I can duplicate the behavior you report with both 8.0.3 and 8.1a2
    and protclsh. Interestingly,  not only does the loop perform wrong, but
    the interpreter doesn't seem to care that $i isn't defined during the
    parsing phase. It looks like the byte compiler is just assuming the body
    shouldn't go through substitution regardless of the quoting (shame on
    it!). 
    
    As proof, modifying the code to be this:
    
        for {set i 0} {$i < 10} {incr i} [list puts $i]
    
    ... gives the same (broken) results. Interestingly, this only seems to
    happen if the variable in the print statement is the same as the loop
    variable. For example, if I change the command to be "puts $z" I get the
    expected error message "can't read "z": no such variable". 
    
    
    This still exists in 8.2b3.  See related 1669 for detailed evaluation. 
    -- 08/13/1999 hobbs
    
  4. foundin changed to: "obsolete: 8.0.3"
  5. priority changed to: "5 Medium"
  6. resolution changed to: "None"
  7. status changed to: "Open"
  8. submitter changed to: "nobody"
  9. subsystem changed to: "10. Objects"
  10. title changed to:
    for loop with body "puts $i" does not compile correctly
    
  11. type changed to: "Bug"