Consistency, Templates, Constructors, and D3

foobar foo at bar.com
Mon Aug 27 07:53:26 PDT 2012


On Monday, 27 August 2012 at 13:59:53 UTC, F i L wrote:
> F i L wrote:
>> auto a = Point(int)[].new(5).new(1, 2);
>
> On second thought, the shorthand might need to require the '[]' 
> syntax:
>
>     auto a = Point(int)[].new(5)
>     a[].new(1, 2)
>
>     // shorthand:
>     auto a = Point(int)[].new(5)[].new(1, 2);

I'd expect something like:

// 1. create Array of 5 elements, default construction
auto a = Array!(T).new(5);

// 2. create Array of 7 elements
// all elements init-ed with the same "this(params)" c-tor
auto b = Array!(T).new(7, params);

// 3. create Array of 3 elements, init-ed via function
auto c = Array!(T).new(3, (length) {...});

Regarding syntax [sugar], the above would translate to:

1. auto a = T[].new(5);
2. auto b = T[].new(7, params);
3. auto c = T[].new(7, (length) {...});

For multidimensional arrays the above can be generalized as 
following:
// first parameter is a tuple
// should there be syntax sugar for this?
auto d1 = Array!T.new((6,5,4), params); // 6x5x4 3d array
auto d2 = Array!T.new((6,5,4), (dims) {...}); // 6x5x4 3d array

// "arrays of arrays"
auto d1 = T[][].new(6, T[].new(5, params)); // nest "new" calls
auto d2 = T[][].new(6, (length) {...}); // init sub-arrays inside 
function

// or treat multi "[]" as multidimensional - IMHO this is less 
optimal
// compiler knows how many dimensions there are
auto d1 = T[][][].new(6,5,4, params); // 6x5x4 3d array
auto d2 = T[][][].new(6,5,4, (x,y,z) {...}); // 6x5x4 3d array

for static arrays:
1. auto a = T[5].new();
2. auto b = T[7].new(params);
3. auto c = T[3].new((length) {...});


P.S
I saw in a different language (Nimrod?) the syntax for array!T is 
"[T]" which I like a bit more than D's "T[]" but this isn't that 
important for the discussion at hand.


More information about the Digitalmars-d mailing list