On Thu, Jan 22, 2009 at 7:00 PM, Bill Baxter <wbax...@gmail.com> wrote:
>
> I posted a proposal for how to hide the magic int from the user a
> while back, but my conclusion was that my approach would require AST
> macros in order to give it a reasonable syntax.  With the ast macros
> you'd be able to do something like  yield(i) in the body of your
> foreach, where yield is an appropriately defined macro.
>

Consider the case where you return a value inside a foreach loop:

int find(char[] value)
{
    foreach(k, v; someAA)
        if(v == value)
            return k;
}

How is this implemented?  It puts a local variable in the stack frame
of find(), called __result.  Then the foreach delegate just writes
into that local when it returns (__result = k; return
IM_RETURNING_NOW;), and the compiler-generated cruft returns the value
of that local from find() (switch(...) { case IM_RETURNING_NOW: return
__result; }).

The delegate return status code doesn't have to be any different.
Just have the delegate return a bool (should iteration stop?) and put
its actual return status in the stack frame of the enclosing function.

Reply via email to