The future of foreach

Don Clugston dac at nospam.com.au
Tue Dec 25 07:34:29 PST 2007


Janice Caron wrote:
> On 12/23/07, Hxal <hxal at freenode.d.channel> wrote:
>> What you're suggesting is perfectly possible to do without any language changes.
> 
> Since what I'm suggesting /is/ a language change, it clearly isn't.
> 
> /Obviously/ one can work around the inconsistencies. It's not hard.
> For arrays, for example, one can simply write:
> 
>     for (int i=0; i<a.length; ++i) { a[i] = b[i] * c[i]; }
> 
> (...and in fact, I'll bet good money that that's what most people do).
> The point is, that might not be the most efficient way of doing it. To
> get the most efficient method, you really want to let the compiler,
> not the programmer, choose the "how". That is, after all, the very
> motivation behind foreach in the first place.
> 
> I'm not the first to suggest this. I have it on good authority that
> Andrei suggested the syntax for foreach with multiple loops long
> before this. But it never got implemented, because Walter couldn't
> figure out how to make it interact with opApply.

I don't think that's the main reason. A more fundumental problem is that 
requiring a call to opApply on each foreach iteration is a massive performance 
hit. This is a problem even in a normal use of foreach. Andrei is currently 
trying to come up with a solution.



More information about the Digitalmars-d mailing list