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

aliak something at something.com
Sun Dec 31 13:32:03 UTC 2017


Alo! I'm making a recursive concat function that is similar to 
chain. The following code works:

import std.range: isInputRange;

auto concat(R, V...)(R range, V values) if (isInputRange!R) {
     import std.range: chain, ElementType;
     static if (V.length) {
         static if (isInputRange!(V[0])) {
             return range.chain(values[0]).concat(values[1..$]);
         } else static if (is(V[0] == ElementType!R)) {
             return range.chain([values[0]]).concat(values[1..$]);
         } // add an else assert here.
     } else {
         return range;
     }
}

But the following does not:

auto concat(R, V...)(R range, V values) if (isInputRange!R) {
     import std.range: chain, ElementType;
     static if (!V.length) {
         return range;
     }
     static if (isInputRange!(V[0])) {
         return range.chain(values[0]).concat(values[1..$]);
     }
     static if (is(V[0] == ElementType!R)) {
         return range.chain([values[0]]).concat(values[1..$]);
     }
}

You get a:

Error: tuple index 0 exceeds 0
Error: template instance range.concat.concat!(Result) error 
instantiating

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.

Is it a current limitation of static if? or a bug? or is 
something like this just not possible because of something I'm 
not seeing?

Cheers



More information about the Digitalmars-d-learn mailing list