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

Jari-Matti Mäkelä jmjmak at utu.fi.invalid
Wed Mar 1 13:40:16 PST 2006


Chris Sauls wrote:
> Jari-Matti Mäkelä wrote:
>> It should be someday also possible to create dynamic arrays of a given
>> length without this much clutter:
>>
>>  type[] array;
>>  array.length = x;
> 
> Currently:
> # type[] array = new type[x];

Thanks, didn't know that yet. Haven't been much around lately. Time to
read through the docs again :)

> 
> I would love to see the ability to do:
> # type[][] array = new type[y][x];
> 

I think some problems will appear when you want to combine different
types of arrays here. E.g. part of the array structure is static, part
of it dynamic.

> As to the subject of array literals, I've always been fond of this syntax:
> # int[] foo = new int[] [1, 2, 3];
> 
> However, it does suffer a couple of problems.  For one, because it uses
> []'s instead of {}'s or ()'s, it makes for a parsing trap.  In most
> cases, this is escapable because we already know the type we are
> expecting (from the decleration before the initializer) so we can just
> count bracket pairs, but what if we want to use 'auto'?
> 
> # auto foo = new int[] [1, 2, 3];
> 

What about dynamic arrays as function parameters? (I need them a lot)

 foo(new int[] [1,2,3]);

> I would say go back to using {}'s, and perhaps prepend struct
> initializers with a 'new' instruction as well.  Of course, if we get
> this 'local' instruction for stack allocating, then one could opt to use
> that instead.

I agree.

-- 
Jari-Matti



More information about the Digitalmars-d mailing list