[Issue 15166] [REG2.069-devel] spurious statement not reachable warning in static foreach loop

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Tue Oct 13 09:03:08 PDT 2015


https://issues.dlang.org/show_bug.cgi?id=15166

Steven Schveighoffer <schveiguy at yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy at yahoo.com

--- Comment #6 from Steven Schveighoffer <schveiguy at yahoo.com> ---
(In reply to Martin Nowak from comment #4)
> A workaround is to use a variable.

I think this may be the right answer.

It boils down to this:

static if(someCondition) return false;
return true;

Which you would normally write with else, but it's not so simple in this case,
because the "else" would be part of the loop.

I'm curious why the return short-circuits the loop. In other words, why aren't
all the "return false" statements besides the first one flagged for
unreachability? Is it because the compiler stops generating the statements? I
mean, if you rewrote as a bunch of static ifs, then wouldn't you have the same
problem?

Another possible answer is to do this:

private bool compare(alias Group1, alias Group2)()
{
    foreach (index, element; Group!(Group1.expand, void).expand)
    {
        static if(index == Group1.expand.length)
            return true;
        else static if (!is(Group1.expand[index] == Group2.expand[index]))
            return false;
    }
}

--


More information about the Digitalmars-d-bugs mailing list