The magic behind foreach (was: Re: Descent 0.5.3 released)
Bill Baxter
wbaxter at gmail.com
Thu Jan 22 15:26:42 PST 2009
On Fri, Jan 23, 2009 at 8:10 AM, Christopher Wright <dhasenan at gmail.com> wrote:
> Ary Borenszweig wrote:
>>
>> If the compiler can transform a "foreach" into an opApply call, passing
>> the foreach body and converting breaks to "return 1" statements... can't
>> opApply be specified as:
>>
>> int opApply(void delegate(ref uint) dg) { // note: delegate returns void
>> }
>>
>> and the compiler transforms the opApply signature to the one that's used
>> now, plus converting each dg call to a call and a check of return value !=
>> 0 and return 1 in that case?
>
> This only fails if you wish to take a particular action when the calling
> code breaks out of iteration. This is not such a large use case that I think
> it worth preserving.
It's not?
foreach(i; things) {
if (i==a) continue;
if (i==b) break;
if (i==d) return;
if (i==c) goto somewhere;
}
Those are all fairly common things to do from inside the 'dg' call.
The int is how the compiler distinguishes which case got you out of
the dg.
--bb
More information about the Digitalmars-d
mailing list