Regurgitated proposal: Make loop conditions part of the body's scope

Bill Baxter dnewsgroup at billbaxter.com
Wed Aug 1 12:43:16 PDT 2007


downs wrote:
> I realize this has been requested before, by me and a few others, gained 
> the votes of a significant amount of people here, been completely 
> ignored by Walter (no offense meant, I know he can't read every 
> proposal), you know how it goes. The reason I'm bringing it up again is 
> that it could potentially, and implicitly, help (or even completely) fix 
> a quite serious bug I discovered this morning.
> 
> To recap: the proposal is to make the parameter ("()") part of some 
> common constructs such as while, do/while and with (I'll get to that in 
> a second) part of the scope of the actual loop body - this would allow, 
> for instance, checking the value of an inner variable in the condition 
> of a do/while loop.
> Disadvantages: it breaks compatibility with C++ in cases which are 
> forbidden anyway (shadowing declarations). .. That's all I can think of. 
> As far as I know, it doesn't change the behavior of existing code.
> 
> Now, for the bug.
> Consider the following example program.
> 
> scope class test { this() { writefln("this"); } ~this() { 
> writefln("~this"); } }
> void main() {
>         writefln("Pre");
>         with (new test) { writefln("Inside With"); }
>         writefln("Post");
> }
> 
> What would be expected: Pre  this  Inside With  ~this  Post.
> What happens: Pre  This  Inside With  Post  ~this.
> Okay, I thought, so the new test is in the outside scope. Sucks, but 
> workable. I changed it to
> 
>     { with (new test) { writefln("Inside With"); } }
> 
> ...
> I got the same results.
> Turns out constructing an instance of a scoped class as the parameter of 
> a with construct makes the compiler COMPLETELY ignore the scope.
> And I can't help thinking, if the with loop's parameters were part of 
> the inner scope, that bug _probably_ wouldn't have happened. Which is 
> why I'm holding off on a bug report until there's some feedback on what 
> the compiler should do in this situation.
> 
> With greetings and stuffies,
>  --downs


I'm pretty sure variables are within the scope of for loops etc.  At 
least I'm 99% sure this is Ok:

   for(int i=0;i<10;i++) { writefln(i); }
   for(int i=0;i<10;i++) { writefln(i); }

So the scope of the i *is* limited to just the body of the for loop 
normally.  'Scope' seems to defeat that somehow.  Actually I thought 
even with scope classes you still needed to say 'scope' at the point of 
use or else get an error.  So it could be that the root of the error is 
that D is letting you create a scope class in a non-scope way, when it 
shouldn't.

--bb



More information about the Digitalmars-d mailing list