Comment #0 by andrej.mitrovich — 2012-02-06T14:12:31Z
import std.file;
void main()
{
foreach (i; 0 .. 10)
return; // ok
foreach (string entry; dirEntries(".", SpanMode.shallow))
{
return; // Error: long has no effect in expression (0)
// test.d(10): Error: cannot return non-void from void function
}
}
This caught me off-guard. In fact:
import std.file;
import std.stdio;
void main()
{
writeln(test()); // writes bar
}
string test()
{
foreach (string entry; dirEntries(".", SpanMode.shallow))
{
return "foo";
}
return "bar";
}
The foreach loop *is* entered, but the return statement just makes the foreach loop break out, and then the 'return "bar";' statement is reached instead. This almost looks like a hole in the design of opApply?
Comment #1 by andrej.mitrovich — 2012-04-19T19:07:56Z
The second sample is fixed because it was a bug in dirEntry's opApply function.
There's just one thing left:
import std.file;
void main(){
// Error: long has no effect in expression (0)
foreach (_; dirEntries(".", SpanMode.shallow)) {
return;
}
}
void test() {
// works fine
foreach (_; dirEntries(".", SpanMode.shallow)) {
return;
}
}
Why does the return fail if we're in main()?
Comment #2 by k.hara.pg — 2012-04-20T01:37:22Z
Reduced test case:
struct S {
int opApply(int delegate(string) dg) {
return 0;
}
}
void main() {
foreach (_; S()) {
return;
// Error: long has no effect in expression (0)
// test.d(8): Error: cannot return non-void from void function
}
}
Pull request:
https://github.com/D-Programming-Language/dmd/pull/892
Comment #3 by github-bugzilla — 2012-04-20T12:04:41Z