On Friday, 1 April 2016 at 01:21:32 UTC, Walter Bright wrote:
On 3/16/2016 4:18 AM, Johan Engelen wrote:
I've found discussions, but not an actual "recommended"
solution for the
problem of "statement is not reachable" warnings in templates
with early
returns, e.g.:
```
bool nobool(T...)() {
foreach (i, U; T) {
static if (is(U == bool)) {
return false;
}
}
return true; // emits "Warning: statement is not
reachable"
}
bool nobool(T...)() {
bool result = true;
foreach (i, U; T) {
static if (is(U == bool)) {
result = false;
break;
}
else
{
...
}
}
return result; // emits "Warning: statement is not
reachable"
}
Note that the optimizer will remove the dead loads.
Actually, I would expect every call of this fuction to be
replaced by a boolean constant, since the result depends only on
compile-time information.
What happened to "obvious code should be correct"? Can we try to
find a solution that doesn't look like a hack? I don't think that
we should ask users of the language to uglify their code just to
workaround a deficiency in the compiler. Instead, we should
improve the lowering of the static foreach construct so the
information that it is syntactically a loop is preserved after
loop unrolling.