guard clause style static if
Timoses
timosesu at gmail.com
Tue Jul 10 13:02:15 UTC 2018
On Tuesday, 10 July 2018 at 12:10:27 UTC, Jonathan M Davis wrote:
> On Tuesday, 10 July 2018 05:38:33 MDT kdevel via
> Digitalmars-d-learn wrote:
>> I would like to suggest an extension of the language by
>> introducing
>>
>> static return Expression_opt;
>>
>> which shall have the effect of a return plus that the
>> remaining lines in the current block are treated as if they
>> were enclosed in an else block.
>
> Well, you can propose it, but it really doesn't fit with how
> static if works. static if doesn't have anything to do with
> control flow, whereas what you're proposing here would, which
> would arguably make it that much more confusing. I confess that
> I don't understand what the problem is with simply adding an
> else block. It's simple, and it works right now without any
> language changes.
>
> - Jonathan M Davis
Actually, it's kind of controlling "compile-time control flow".
If you had
auto func(T)(T args)
{
static if (T.length == 0)
static return;
return args[0];
}
then `static return;` would instruct the compiler to skip the
rest of the code and only insert a `return;`, omitting any code
that follows. So the result in above case with `T.length == 0`
would be:
void func()
{
return;
}
To me that sounds like a very nice feature, considering that I
often found myself in the same situation where I didn't feel like
the static `else` statement was really necessary (comparing to
the usual way to control flow (at run-time) with convential
if-else statements).
More information about the Digitalmars-d-learn
mailing list