www.digitalmars.com         C & C++   DMDScript  

D - Itterator construct.


I saw in the 'Future' section of the D documentation the following:

    1.   A generic way to iterate across a collection, i.e. a foreach 
construct instead of a for loop. A reverse iterator will be needed, too.

In the language I wrote over Christmas, I added support for itterators 
in a way that I like better than the support in C++ or Java.  IMO 
Itterators are important enough to justify new syntax.  Here's what I did:

In a class, you declare a method like:

Class Foo {
     Bar firstBar;
     loop forward(inout Bar bar) {
         for(bar = firstBar; bar != null; bar = bar.nextFooBar) {

Class Bar {
     Bar nextFooBar;
     Foo owner;

The foreach statement looks like:

testLoop(Foo foo)
     Bar bar;
     foreach(foo.forward, bar) {
         /* do something with bar here */

Bill Cox
Jan 25 2003