hacks on opApply

monkyyy crazymonkyyy at gmail.com
Wed Aug 27 20:34:22 UTC 2025


spec doesn't define this behavior as far as I can tell

```d
import std;
auto loop(){
	struct foreach_{
		int opApply(int delegate(int i) dg){
			dg(0).writeln;//1
			dg(1).writeln;//2
			dg(2).writeln;//3
			return 3;
	}}
	return foreach_();
}
unittest{
	a: while(true){
	b: foreach(i;loop()){
		if(i==0){break;}
		if(i==1){break b;}
		if(i==2){break a;}
}}}
```

rn I believe you could pretty reasonably overload the behavior of 
break in a crafted opApply to make a 2d iteration

it should be possible to modify ref indexs and other stateful 
things that are tricky with pure ranges

slight errors produce infinite loops tho and this is like the 3rd 
opApply ive ever written, no idea on stable patterns


More information about the Digitalmars-d mailing list