Auto recursive function

Razvan Nitu via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jan 11 11:23:10 PST 2017


Hi,

I am currently trying to create a function 
makeMultidimensionalArray which allocates memory for a 
multidimensional array. It is very similar with [1],
the difference being that it is uninitialized. Here is the code:

auto makeMultidimensionalArray(T, Allocator)(auto ref Allocator 
alloc, size_t[] lengths)
{
     if (lengths.length == 1)
     {
         return makeArray!T(alloc, lengths[0]);
     }
     else
     {
         alias E = typeof(makeMultidimensionalArray!T(alloc, 
lengths[1..$]));
         auto ret = makeArray!E(alloc, lengths[0]);
         foreach (ref e; ret)
             e = makeMultidimensionalArray!T(alloc, lengths[1..$]);
         return ret;
     }
}

The lengths[] specifies the lengths for each dimension. The 
problem with this code is that auto is going to be evaluated to 
T[] for the first time and when it
recurs, creating T[][] I get the error "mismatched function 
return type inference of T[][] and T[]". Is there a way to 
surpass that? I saw that in [1]
the recursive call is done by prefixing the function name with a 
'.'; I tried that but it doesn't work. I must be missing 
something, any ideas?

Thanks,
RazvanN

[1] 
https://github.com/dlang/phobos/blob/master/std/experimental/ndslice/slice.d#L834


More information about the Digitalmars-d-learn mailing list