Make sense? No; of course not. And 'somewhere' isn't useful for problem solving. So I resorted to doing printf debugging in the language itself. 3 minutes later and I've a much clearer picture of at least where the problem occurs:
public static yields<int> take(int amount, IEnumerable<int> fromCollection){ local int ix = 0; foreach( int rtn in fromCollection ){ print "start of take foreach.\r\n"; if( !(ix < amount) ){ print "before take return.\r\n"; return(); } print "before take yield. returning rtn: " rtn " \r\n"; yield rtn; print "after take yield. rtn is still: " rtn " \r\n"; ix = ix + 1; print "end of take foreach.\r\n"; } print "exit take.\r\n";}public static yields<int> allInt(){ local int i = 0; while(true){ print "before allInt Yield. yielding i: " i " \r\n"; yield i; print "after allInt Yield. i is still: " i " \r\n"; i = i + 1; }}public static void main(){ foreach( int x in take(5,allInt()) ){ print "X: " x " \r\n"; }}
before allInt Yield. yielding i: 0 start of take foreach.before take yield. returning rtn: 0 after allInt Yield. i is still: 0 before allInt Yield. yielding i: 1 X: 0 *snip*
Yuck. The core of the problem is at output line 3->4. take's yield is supposed to return to main so the result gets printed out. Instead, it's moving the execution pointer into allInt and causing that to run again. It does it after setting the return value though, since 0 is eventually printed. So probably in the foreach block/loop, it thinks it's supposed to restart something rather than exiting back to main.
Now to find out why...