DMD 0.174 release

Stewart Gordon smjg_1998 at yahoo.com
Fri Nov 17 12:58:33 PST 2006


Walter Bright wrote:
> Stewart Gordon wrote:
<snip>
>> - the idea of the same variable being declared multiple times in the 
>> same instance of being in the same scope is alien to many
> 
> ? You cannot declare the same variable multiple times in the same scope.

What I meant is that, in order for the count variable to continue across 
iterations, it would imply that the control enters and leaves the scope 
only once for the whole for loop.  And so

     for (int x = 0; x < 3; x++) {
         int y = x*x;
         writefln(y);
     }

would seem equivalent to

     {
         int x = 0;
         int y0 = x * x;
         writefln(y0);
         x++;
         int y1 = x * x;
         writefln(y1);
         x++;
         int y2 = x * x;
         writefln(y2);
         x++;
     }

i.e. in each instance a new y is declared, hiding the previous, but they 
don't go out of scope till the end of the loop.

OK, so there's also the goto, which could be used to go back to before a 
declaration statement and execute it again.  JTAI, I suppose that 
explains something.  You're not really redeclaring it, but merely 
resetting its value.  So while declarations may be anywhere within a 
function, at the internal level the placement of a declaration is merely 
the point at which the variable is given its initial value.

>> - there are also static local variables, which aren't reset each time
> 
> Again, this behaves just like C++, and should not be surprising.
> 
>> Moreover, your claim that the _body_ of a for loop doesn't create a 
>> scope would also mean that ScopeGuardStatements and scope (fka auto) 
>> object references within the body are not processed at the end of each 
>> iteration, but saved until the end of the whole for loop.  Absolutely 
>> not the behaviour I'm experiencing.
> 
> The for statement itself generates a scope, not the { }. This behaves 
> exactly like the for statement in C++.

Does the flow of control enter and leave this scope only once for the 
whole for loop or once for each iteration?

- If once for the whole loop, how would you explain the behaviour of 
scope guards that I'm experiencing?
- If for each iteration, then how does the counter variable (if declared 
in the Initialize) keep hold of its value between iterations?

Stewart.

-- 
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/M d- s:-@ C++@ a->--- UB@ P+ L E@ W++@ N+++ o K-@ w++@ O? M V? PS- 
PE- Y? PGP- t- 5? X? R b DI? D G e++++ h-- r-- !y
------END GEEK CODE BLOCK------

My e-mail is valid but not my primary mailbox.  Please keep replies on 
the 'group where everyone may benefit.



More information about the Digitalmars-d-announce mailing list