Working with ranges: mismatched function return type inference

orip via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Oct 11 00:55:36 PDT 2016


I get "Error: mismatched function return type inference" errors 
with choosing the return type for functions that work on ranges 
using, e.g, std.algorithm or std.range functions, but have 
different behavior based on runtime values. The return type is 
always a range with the same underlying type.

Here's an example:

auto foo(int[] ints) {
   import std.range;
   if (ints.length > 10) {
     return chain(ints[0..5], ints[8..$]);
   } else {
     //return ints; // Error: mismatched function return type 
inference of int[] and Result
     return chain(ints[0..0], ints[0..$]); // This workaround 
compiles
   }
}

Is there a compatible return type that can be used, or some other 
workaround?
I couldn't find one when searching for the error or looking at 
the phobos source code.

Thanks! orip


More information about the Digitalmars-d-learn mailing list