The future of foreach

Craig Black craigblack2 at cox.net
Sun Dec 23 09:36:17 PST 2007


> Once step three is in place, structs and classes will no longer need a
> toArray() function, and (better still) the mechanism will work even
> for collections which /can't/ return an array, such as linked lists.
> At this point we'll be able to do
>
>    List!(Widget) a,b,c;
>    foreach(ref x;a)(y;b)(z;c) { a = b.someFunction(c); }
>
> Thoughts?

Once we have better support for structs, we can have struct iterators. 
Iterators are superior to the current approach because they are more 
efficient.  Iterating through multiple collections in lockstep would be 
possible using a specialized iterator.  If anyone wants me to lay out a 
detailed design I can do it, but otherwise I won't waste my time.

As far as letting the compiler decide the most efficient way to do it, I 
don't think that it's necessary for collections.  Struct iterators would be 
very efficient.

-Craig 




More information about the Digitalmars-d mailing list