foreach, an analogy

Bill Baxter dnewsgroup at billbaxter.com
Wed Oct 18 20:56:26 PDT 2006


Gregor Richards wrote:

> The bigger door is 'for'.  'foreach' is nothing but a convenient wrapper 
> around 'for'.  And don't you OOphiles go telling me that your fancy 
> class foo that has iteration /needs/ 'foreach':
>   for (auto bar = foo.begin(); !(bar is null); bar = foo.iterate(bar))
> Is it less pretty than foreach?  Yeah.  That's why foreach exists.  

Actually, you've got a point there, sort of.  Iterating over *arrays* 
with for is really really not that bad.

    for (int i=0; i<x.length; i++) {
       something(x[i]);
    }

That kind of thing never made me want to run screaming to find a new 
career while doing C++ programming.

But this:
   for (auto bar = foo.begin(); !(bar is null); bar = foo.iterate(bar))
   {   something(bar)
   }
would.  Yeh, the auto makes it a big improvement over current C++.  But 
still, it's annoyingly long for something that happens so frequently.

I really can't stand ugly STL for's anymore after working with Python 
for a while.   But Python doesn't do everything I need.  It's not fast. 
  That's why I'm here.

--bb



More information about the Digitalmars-d-announce mailing list