static if and early exit from function doesn't seem to work?

aliak something at something.com
Mon Jan 1 18:49:11 UTC 2018


On Sunday, 31 December 2017 at 13:47:32 UTC, Adam D. Ruppe wrote:
> On Sunday, 31 December 2017 at 13:32:03 UTC, aliak wrote:
>> So it seems it tries to compile the statements below the check 
>> on V.length even though it's guaranteed to be true and there's 
>> a return statement inside the if.
>
> Yeah, static if includes or excludes code independently at 
> compile time.
>
> So what you wrote there would be like, assuming the first to 
> static ifs pass:
>
> auto concat(R, V...)(R range, V values) if (isInputRange!R) {
>     import std.range: chain, ElementType;
>         return range;
>         return range.chain(values[0]).concat(values[1..$]);
> }
>
>
> The code is still there, even if it isn't reached due to an 
> early return, and thus still must compile.
>
> Using else static if means it won't be generated.

Ah ok, thanks! So it is intended behavior. I wonder if treating a 
return like a static else would be a good idea though. I at least 
can't see how it would break anything at this time.




More information about the Digitalmars-d-learn mailing list