Using "[]" for empty array (instead of null)

Jari-Matti Mäkelä jmjmak at utu.fi.invalid
Wed Mar 1 08:30:11 PST 2006


Lionello Lunesu wrote:
> Futhermore, "[]" does not suffer from the same problems. What's the type of 
> "[2]"? Will "ar=[2]" copy the data, dup it or just point to it?
> 

BTW, is there a very good reason why D uses []'s instead of {}'s? This
makes it hard to create intuitive/consistent array literals in the future.

For example, what should this do?

 int[] foobar;
 <some code here>
 foobar = [2];

a) replace foobar contents with the element '2'
b) create a new dynamic array that contains '2'
c) create a new dynamic array with a length of 2

In Java all arrays are objects so it's convenient to create a new array
with 'new' but how should it be done in D? I guess the syntax should be
left open so that it's possible to fill in the array literals in the future:

 Foobar a,b,c;
 <some code here>
 Foobar[] table = [ a, b, c ]; or
 Foobar[] table = { a, b, c }; or even
 Foobar[] table = new { a, b, c };

It should be someday also possible to create dynamic arrays of a given
length without this much clutter:

 type[] array;
 array.length = x;

It should be like

 type[] array = type[x];

or something similar.

Multidimensional arrays are a bit difficult too:

 type[][] array;
 array.length = y;
 foreach(inout type[] row; array) row.length = x;

I would be very happy, if it could be just

 type[][] array = type[y,x]; or
 type[][] array = type[y][x]; or even
 array.length = y, x;


-- 
Jari-Matti



More information about the Digitalmars-d mailing list