The demise of T[new]

Yigal Chripun yigal100 at gmail.com
Mon Oct 19 02:37:50 PDT 2009


Walter Bright Wrote:

> The purpose of T[new] was to solve the problems T[] had with passing T[] 
> to a function and then the function resizes the T[]. What happens with 
> the original?
> 
> The solution we came up with was to create a third array type, T[new], 
> which was a reference type.
> 
> Andrei had the idea that T[new] could be dispensed with by making a 
> "builder" library type to handle creating arrays by doing things like 
> appending, and then delivering a finished T[] type. This is similar to 
> what std.outbuffer and std.array.Appender do, they just need a bit of 
> refining.
> 
> The .length property of T[] would then become an rvalue only, not an 
> lvalue, and ~= would no longer be allowed for T[].
> 
> We both feel that this would simplify D, make it more flexible, and 
> remove some awkward corner cases like the inability to say a.length++.
> 
> What do you think?

I think that Arrays and AAs need to be removed from the language. 
I prefer to have a collections framework as part of Phobos without special cases in the language for specific containers. 

The only thing that needs to remain on the language side is support for user defined literals and operators so that it would be straight forward to use (as opposed to the clumsiness of C++). This syntax sugar is the *only* benefit of built in types and this should be possible to have without cluttering the language.

such a framework would ideally support polymorphism. 

List!(Foo) list = new Array!(Foo); // instead of Foo[new]

auto arr = new Array!(Foo);
...
auto slice = arr[1..4]; // slice is a Range as in Andrei's Ranges in Phobos

Foo[] a; // compile-error Foo[] is not a type, use Range!Foo instead

Foo[3] a; // this is still perfectly legal






More information about the Digitalmars-d mailing list