[Issue 13556] inconsistent 'new' syntax for arrays

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sat May 30 17:29:35 PDT 2015


https://issues.dlang.org/show_bug.cgi?id=13556

--- Comment #10 from dennis.m.ritchie at mail.ru ---
(In reply to Ketmar Dark from comment #9)
> (In reply to dennis.m.ritchie from comment #7)
> > I suggest to implement such a syntax for declaring multidimensional arrays:
> > 
> > auto array = new int[11](4, 8, 6, 13 /* .... The length of the other
> > subarrays on request */);
> 
> you can do almost the same with templates.
> 
> auto DNew(T, A...) () if (A.length > 0) {
>   template A2S(A...) {
>     import std.conv : to;
>     static if (A.length == 0)
>       enum A2S = "";
>     else
>       enum A2S = to!string(A[0])~","~A2S!(A[1..$]);
>   }
>   import std.array : replicate;
>   return mixin("new "~T.stringof~"[]".replicate(A.length)~"("~A2S!A~")");
> }
> 
> 
> void main () {
>   import std.stdio;
>   auto a = DNew!(int, 5, 6, 7);
>   a[2][3][4] = 42;
>   writeln(a[2][3][]);
> }

Yes, do that D is really easy!

Then why in the D still has not gotten rid of this hardcore syntax :)

auto a = new int[][][](5, 6, 7);

Surely this is an attempt to maintain compatibility with C?

--


More information about the Digitalmars-d-bugs mailing list