Ideas regarding flow control and loops

Bruce Adams tortoise_74 at yeah.who.co.uk
Sun Nov 4 02:01:41 PST 2007


BCS Wrote:

> Reply to Daniel,
> 
> > downs wrote:
> > 
> >> Marco Aurélio wrote:
> >> 
> >>> 2 - for .. finally, while .. finally:
> >>> 
> >>> This would allow having something like:
> >>> 
> >>> while(someCondition)
> >>> {
> >>> DoSomething();
> >>> } finally {
> >>> DoOtherThing();
> >>> }
> >>> The "finally" block would be called at the end of the repetition,
> >>> only if no "break" was used. This may not seem useful at first, but
> >>> I think can reduce the number of flags needed to implement various
> >>> algorithms, making the code faster and more elegant. I'm not sure if
> >>> this is already possible with scope guards.
> >>> 
> >> I like that. :) vote +1
> >> --downs
> > else would also be nice.
> > 
> > foreach( foo ; bar )
> > DoSomethingWith(foo);
> > finally
> > DoSomethingAfterwards();
> > else
> > DoSomethingElseSinceBarIsEmpty();
> > But maybe that's just me.
> > 
> > -- Daniel
> > 
> 
> 
> I'd rather an extension of the scope syntax
> 
> while(cond)
> {
>    scope(last) DoOnCondFailed();
>    scope(break) DoOnBreak(); // or any explicet quit
>    scope(skip) DoIfCondNeverPasses();
>    scope(first) goto SkipSomeStuff;
>    ...
> }
>

Lets take these one at a time:-

scope(last):  - totally useless assuming I understood the meaning

while(cond) 
{
  
}
DoOnCondFailed();

scope(skip):  - saves you one conditional - not very useful

if (!cond)
   DoIfCondNeverPasses();
else do
{
   ...
}
while(cond);

scope(break): - saves you a function call or two.

while(cond)
{
  ...
  if (cond2) DoOnBreak(); break;
  ...
  if (cond3) DoOnBreak(); break;
  ...
}

scope(first):

if (cond)
{
  DoFirstTimeOnly();
}
while(cond)
{
  ...
}

goto is evil so you want the opposite too

scope(notonfirst):

while(cond)
{
   static bool firstTime = false;
   if (firstTime==false) { firstTime = true; body1(); }
   body2();
}

For this case there is slight justification for something. but that
something is a notfirst() functor.

class notfirst
{
private:
   bool first;
   delegate doOnFirst;
   delegate doOnSubsequent;
public:
   notfirst(delegate doOnFirst_, 
              delegate doOnSubsequent_):
      first(true) 
      doOnFirst(doOnFirst_),
      doOnSubsequent(doOnSubsequent_)
   {}

   void run() {
      if (first) 
      {  
         doOnFirst();
         first = false;
      }
      else doOnSubsequent();
   } 
};

while(cond)
{
   doOnFirst(body1(); body2());
}


What is this equivalent to in the worst case scenario that you need all four?

if (!cond)
   DoIfCondNeverPasses();
else
{
  DoFirstTimeOnly();
  do
  {
     doOnFirst(body1(); 
     {
         ...
         if (cond2) DoOnBreak(); break;
         ...
         if (cond3) DoOnBreak(); break;
         ...
      }
  }
  while(cond);
}
DoOnCondFailed();

On balance this doesn't seem like a necessary or really useful syntax improvement to me.

Regards,

Bruce.
to me.



More information about the Digitalmars-d mailing list