Auto recursive function

Ignacious via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jan 11 13:10:39 PST 2017


On Wednesday, 11 January 2017 at 19:23:10 UTC, Razvan Nitu wrote:
> 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


This is probably not possible. You are trying to have multiple 
return types for the same function. You are thinking that each 
recursive call is a new template but that doesn't seem to be the 
case.


Instead, maybe try using string mixins to generate the 
allocations. Should be quite easy and will work.

You could also try to use a helper function that you pass the 
fully declared array(all dimensions) and the helper function then 
allocates each dimension recursively... The difference is that 
you are not passing around/returning sub-arrays so you don't have 
to worry about type mismatches.







More information about the Digitalmars-d-learn mailing list