Bug or am I getting things wrong

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jan 7 01:31:51 PST 2016


On 01/06/2016 11:51 PM, Q. Schroll wrote:
> In the listing below the commented line should do exactly the same thing
> as the one above.
>
> /// DimArr!(i, T) ==> T[][]...[] i times.
> template DimArr(size_t i, T)
> {
>      static if (i == 0)  alias DimArr = T;
>      else                alias DimArr = DimArr!(i - 1, T)[];
> }
>
>
> struct Array(T, size_t rk)
>      if (rk > 1)
> {
>      private size_t[rk] dims;
>      /+ ... +/
>      auto toNestedArray() const
>      {
>          import std.range : iota;
>          import std.format : format;
>          enum dimsIndexed = `%(dims[%d]%|, %)`.format(dims.length.iota);
>          auto result = mixin(`new DimArr!(rk, T)(` ~ dimsIndexed ~ `)`);
>       // auto result = new DimArr!(rk, T)(mixin(dimsIndexed)));
>          /+ ... +/
>          return result;
>      }
> }
>
> The one above does exactly what I want, but the lower one only uses the
> last dimension for some reason. I found out by using these pragmas
>
>      pragma(msg, "'", dimsIndexed, "'");
>      pragma(msg, ( new DimArr!(rk, T) ( mixin(dimsIndexed) ) ).stringof);
>
> Can someone please tell me what I'm getting wrong here, or is this a bug?

I don't see any difference with dmd v2.069.2. Both lines print the 
following:

'dims[0], dims[1], dims[2], dims[3], dims[4], dims[5], dims[6], dims[7], 
dims[8], dims[9]'
new int[][][][][][][][][][](this.dims[9])

Here is the program:

/// DimArr!(i, T) ==> T[][]...[] i times.
template DimArr(size_t i, T)
{
     static if (i == 0)  alias DimArr = T;
     else                alias DimArr = DimArr!(i - 1, T)[];
}


struct Array(T, size_t rk)
     if (rk > 1)
{
     private size_t[rk] dims;
     /+ ... +/
     auto toNestedArray() const
     {
         import std.range : iota;
         import std.format : format;
         enum dimsIndexed = `%(dims[%d]%|, %)`.format(dims.length.iota);
         auto result = mixin(`new DimArr!(rk, T)(` ~ dimsIndexed ~ `)`);
         // auto result = new DimArr!(rk, T)(mixin(dimsIndexed));
         /+ ... +/

     pragma(msg, "'", dimsIndexed, "'");
     pragma(msg, ( new DimArr!(rk, T) ( mixin(dimsIndexed) ) ).stringof);

         return result;
     }
}

void main() {
     auto a = Array!(int, 10)();
     import std.stdio;
     writeln(a.toNestedArray());
}

Ali



More information about the Digitalmars-d-learn mailing list