The magic behind foreach (was: Re: Descent 0.5.3 released)

Jarrett Billingsley jarrett.billingsley at gmail.com
Thu Jan 22 21:49:25 PST 2009


On Fri, Jan 23, 2009 at 12:30 AM, Bill Baxter <wbaxter at gmail.com> wrote:
>
> I see what you're saying now.  So the user has to manipulate a bool
> return value instead of an int?
>
> Then you could have
>
> void opApply(bool delegate(ref int) dg) {
>      foreach(i; internal_) {
>            if (dg(i)) return;
>      }
> }
>
> Yep, that looks like an improvement to me.

Right.  Granted, it's not perfect - it's still possible to write an
opApply that doesn't respect breaks/returns/gotos out of the loop -
but it's much more straightforward.

A "perfect" opApply would probably look like:

void opApply(void delegate(ref int) dg)
{
    foreach(i; mData)
        dg(i);
}

but it would probably have to abuse exceptions to accomplish this,
which doesn't sound like a great idea to me.



More information about the Digitalmars-d mailing list