Skip to content

[Statements] "foreach over delegates" has suspicious nested delegate with return 0 #4319

@crazymonkyyy

Description

@crazymonkyyy

I know if return 0 is bug prone as return 1 and havnt looked for an example of it breaking labeled break, but......

foreach (x; &myLoop) that & is nesting a delegate of delegates; which suggests when this code was written is was someone searching for something that "just werked" (probably copy and pasted from an opApply code example ) rather than the simplest/idiomatic pattern of what the feature can do.

code im currently playing with:

#!opend test app
import std;
auto toiter(R)(ref R r) if (isInputRange!R){
	return (int delegate(ref ElementType!R) dg){
		int ret;
		while( ! r.empty){
			ret=dg(r.front);
			r.popFront;
			if(ret){return ret;}
		}
		return ret;
	};
}
auto toiter(T)(ref T t) if ( ! isInputRange!T){
	return (int delegate(ref T) dg){
		return dg(t);
	};
}
unittest{
	auto foo=iota(9).toiter;
	foo=10.toiter;
	auto bar=iota(9).toiter;
	excape:foreach(i;foo){
		foreach(j;bar){
			writeln(i,',',j);
			if(j==10){ break excape;}
	}}
	"---".writeln;
	foo=iota(9).toiter;
	bar=10.toiter;
	excape2:foreach(i;foo){
		foreach(j;bar){
			writeln(i,',',j);
			if(j==10 && i==2){ break excape2;}
	}}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions