The future of foreach

Janice Caron caron800 at googlemail.com
Sun Dec 23 06:07:17 PST 2007


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. This is a suggestion
about how to achieve that.

The suggestion is, specifically, a language change - *so that the
compiler can decide* how best to do it.



More information about the Digitalmars-d mailing list