guard clause style static if

kdevel kdevel at vogtner.de
Tue Jul 10 11:38:33 UTC 2018


On Tuesday, 10 July 2018 at 00:11:27 UTC, Steven Schveighoffer 
wrote:
> On 7/7/18 7:28 AM, kdevel wrote:
>> It appears not to be possible to use static if in "guard 
>> clause style" as in
>> 
>>     void bar (T ...) (T args)
>>     {
>>        static if (args.length == 0)
>>           return;
>> 
>>        writeln (args [0]);
>>        return bar (args [1 .. $]);
>>     }
>> 
>> Is this intended?
>
> Yes.
>
> Try just a normal if -- it will have the same effect (the 
> optimizer will eliminate the dead code), but will compile.

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.

> Of course, you have to fix your second part to only return bar 
> if args.length > 0!

This violates DRY. Same problem as with the template constraints 
version where the condition used in every special case has to be 
repeated in the general case.


More information about the Digitalmars-d-learn mailing list