hacks on opApply
Meta
jared771 at gmail.com
Thu Sep 10 07:59:05 UTC 2026
On Wednesday, 27 August 2025 at 20:34:22 UTC, monkyyy wrote:
> 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;}
> }}}
> ```
What about continue? Does it work and return a value? Going off
what Steven said, I assume it'd be 0.
> 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