guard clause style static if

Jonathan M Davis newsgroup.d at jmdavisprog.com
Tue Jul 10 12:07:07 UTC 2018


On Tuesday, 10 July 2018 05:52:59 MDT kdevel via Digitalmars-d-learn wrote:
> On Saturday, 7 July 2018 at 13:03:32 UTC, rikki cattermole wrote:
> > void func() {
> >
> >     return;
> >
> >     func2();
> >
> > }
> >
> > Which is clearly an error. Hence why you need to add else block.
>
> There is no error in this generated code because func2 is
> unreachable.

If you don't get an error with code like

void func()
{
    return;
    func2();
}

then you're not compiling with -w. A _lot_ of projects compile with -w
(IIRC, it's the default for dub). So, unless you're dealing with code that
only you are going to be using, and you can guarantee that it will never be
compiled with -w, then you're going to have problems with code like this. If
you want to ensure that your code is going to be usable by other folks, then
you should be compiling with -w.

> That there is a state/stage during compilation in
> which the call to func2 exists and is checked is an
> implementation detail which IMHO shall not be exposed to the user.

It's going to matter if your code doesn't compile, since at that point, the
implementation detail impacts the user. And unreachable code is something
that won't compile for most projects.

- Jonathan M Davis





More information about the Digitalmars-d-learn mailing list