Top 5

Steven Schveighoffer schveiguy at yahoo.com
Fri Oct 10 09:06:38 PDT 2008


"Benji Smith" wrote
> Andrei Alexandrescu wrote:
>> new T[x] is a brain-dead syntax that I wish Walter hadn't imported in the 
>> first place.
>
> Really? I think it's very valuable.
>
> The "new T[x]" syntax lets you construct an array as an RValue. Without 
> that syntax, you have to declare an array before using it.
>
>    // nice
>    x.setOutputBuffer(new char[64]);
>
>    // not so nice
>    char[64] buffer;
>    x.setOutputBuffer(buffer);
>
> Personally, I'd love to see the distinction between static arrays and 
> dynamic arrays disappear. (The compiler can do whatever it wants behind 
> the scenes, but usually I just don't care which is which, and I'd prefer a 
> unified syntax.)
>
> I think *all* arrays should be declared like this:
>
>    T[] array = new T[n];
>
> If "n" is known it compile time, then D can use CTFE to create a static 
> array, and if "n" isn't known until runtime, it can create a dynamic 
> array. But as the user, I don't want to care which is which.

What if n is 10000?  It's small enough that it could be stack allocated, but 
large enough that you might not want it to do that.

>
> (And I don't see how the distinction in the type-system between T[] and 
> T[3] is useful.)

It tells you the scope of where to allocate data.  But once declared, usage 
should be identical.  Currently there are some quirks in the language that 
make this not true (which should be fixed).  e.g. you can't return static 
arrays from functions.

-Steve 





More information about the Digitalmars-d mailing list