Array literals' default type

Yigal Chripun yigal100 at gmail.com
Sat Oct 10 08:15:55 PDT 2009


On 10/10/2009 16:12, Jarrett Billingsley wrote:
> On Sat, Oct 10, 2009 at 7:33 AM, Yigal Chripun<yigal100 at gmail.com>  wrote:
>>
>> You keep calling these literals "constructor calls' and I agree that that's
>> what they are. My question is then why not make them real constructors?
>>
>> auto a = new int[](x, y, z);
>
> Teehee, that syntax already has meaning in D. Well, that right there
> would give you a semantic error, but "new int[][][](x, y, z)" creates
> a 3-D array with dimensions x, y, and z.
>
> That brings up another point. If you *did* use a class-style ctor
> syntax, how would you list the arguments for a multidimensional array?
> That is, what would be the equivalent of [[1, 2], [3, 4]]?

I know about the current meaning, I was suggesting to change it.

to answer your question -
a) compile-time literals should remain as is so your example of:
[[1, 2], [3, 4]] is still valid.
b) for run-time arrays:
      auto a = new int[][]( (new int[](x, y), new int[](z, w) );
      auto a = new int[][]( Tuple!(x, y), Tuple!(z, w) );
      auto a = new int[][]( [1, 2], [3, 4] );

you can construct a regular array with a literal or a tuple:
int[] a = [1, 2];
int[] b = new int[](x, y);
int[][] is an array of arrays so the rules apply recursively:
both forms can initialize each array in the array of arrays.
tuples of tuples are a shortcut for the second option.

Now, wouldn't it be wonderful if D had provided real tuple support 
without all the Tuple!() nonsense?



More information about the Digitalmars-d mailing list