www.digitalmars.com         C & C++   DMDScript  

D - How break and return is handled in foreach?

reply Ben Y. <Ben_member pathlink.com> writes:
Hi, Walter and Sean.

It looks like the foreach aggregate is using a lamda like function to do the
loop, this is very much like std::for_each in C++.

But std::for_each cannot handle break, return because the loop body is a
functor. Neither can regular functional language construct like foldl.

But how does foreach in D handle this?

It's easy for arrays. But when an apply method is called, how does a break exit
from the apply method? Is it a special exception?

Ben.
Oct 24 2003
parent reply Patrick Down <Patrick_member pathlink.com> writes:
In article <bnbj91$24ki$1 digitaldaemon.com>, Ben Y. says...
Hi, Walter and Sean.

It looks like the foreach aggregate is using a lamda like function to do the
loop, this is very much like std::for_each in C++.

But std::for_each cannot handle break, return because the loop body is a
functor. Neither can regular functional language construct like foldl.

But how does foreach in D handle this?

It's easy for arrays. But when an apply method is called, how does a break exit
from the apply method? Is it a special exception?

Ben.

Look at the foreach documentation here http://www.digitalmars.com/d/statement.html#foreach The delegate generated for the foreach body will return a non zero value when it encounters a break. I'm not sure what happens with a return. <quote> The body of the apply function iterates over the elements it aggregates, passing them each to the dg function. If the dg returns 0, then apply goes on to the next element. If the dg returns a nonzero value, apply must cease iterating and return that value. Otherwise, after done iterating across all the elements, apply will return 0. </quote>
Oct 24 2003
parent reply "Walter" <walter digitalmars.com> writes:
"Patrick Down" <Patrick_member pathlink.com> wrote in message
news:bnbtrs$2iud$1 digitaldaemon.com...
 Look at the foreach documentation here
 http://www.digitalmars.com/d/statement.html#foreach

 The delegate generated for the foreach body will return a non zero
 value when it encounters a break.  I'm not sure what happens with a

 <quote>
 The body of the apply  function iterates over the elements it aggregates,
 passing them each to the dg function. If the dg returns 0, then apply goes

 the next element. If the dg returns a nonzero value, apply must cease

 and return that value. Otherwise, after done iterating across all the

 apply will return 0.
 </quote>

Try various kinds of break, continue, return, and goto's out of a foreach body. You'll see that each is replaced with a return value, and there's a switch statement on these values automagically generated by the compiler to replace the foreach statement.
Oct 24 2003
parent reply Ben Y. <Ben_member pathlink.com> writes:
Try various kinds of break, continue, return, and goto's out of a foreach
body. You'll see that each is replaced with a return value, and there's a
switch statement on these values automagically generated by the compiler to
replace the foreach statement.

what about "return someStruct;"? How does the return value get passed from the delegate to the apply function then to the calling function?
Oct 24 2003
parent "Walter" <walter digitalmars.com> writes:
"Ben Y." <Ben_member pathlink.com> wrote in message
news:bnc3ec$2qcd$1 digitaldaemon.com...
Try various kinds of break, continue, return, and goto's out of a foreach
body. You'll see that each is replaced with a return value, and there's a
switch statement on these values automagically generated by the compiler


replace the foreach statement.


 what about "return someStruct;"? How does the return value get passed from

 delegate to the apply function then to the calling function?

The easiest way to see how this works is to write a simple test program, compile it, and obj2asm it. It works generally like nested local variable access.
Oct 26 2003