Top 5

Sergey Gromov snake.scaly at gmail.com
Fri Oct 10 06:20:30 PDT 2008


Fri, 10 Oct 2008 13:45:41 +0400,
Denis Koroskin wrote:
> On Fri, 10 Oct 2008 03:50:58 +0400, Sergey Gromov <snake.scaly at gmail.com>  
> wrote:
> > I think that the base type should offer both speed and safety by
> > default.  To achieve this I propose to disallow unsafe operations on
> > current arrays, call them slices, and introduce a built-in memory
> > management class, Array(T).  Here's why built-in:
> >
> > void main()
> > {
> >   auto arr = new char[]; // arr is of type Array!(char)
> >   arr.length = 10;       // OK
> >   arr ~= 'c';            // fast
> >   arr ~= "foo bar";      // fast
> >   foo(arr);              // implicit cast to char[]
> >   bar(arr);              // passed by reference
> >                          // "text" is appended
> > }
> >
> > void foo(char[] a)
> > {
> >   char x = a[0];                // OK
> >   char[] word = a[10..14];      // fine
> >   size_t l = a.length;          // 18
> >   a.length = 20;                // error, .length is read-only
> >   a ~= "text";                  // error, append not supported
> >   char[] n = a[15..18] ~ "some" // type of expression is Array!(char)
> >              ~ word             // fast
> >              ~ "baz"            // fast
> >              ;                  // implicit cast to char[]
> > }
> >
> > void bar(Array!(char) a)
> > {
> >   a ~= "text"; // OK
> > }
> >
> 
> The basic idea is good, but I believe that the proposal as is is too much  
> complex. I think Array!(T) should not be casted to T[] implicitly,  
> instead, it could have T[] all() method to be on par with ranges design.
> 
> But then, this reduces usefulness of T[]. When should it be used? What are  
> the benefits of const(T)[] over const(Array!(T))?
> 
> As you prosope, difference between T[] functionality (mutable but not  
> resizable) over Array!(T) (mutable and resizable) is just too narrow.

My T[] is useful when you want to recursively split a megabyte file into 
a couple thousands of tokens, and then modify some of those tokens.  For 
that, your T[] must be lightweight, it must reference a bigger piece of 
data, and it must guarantee not to write anything into memory outside 
its boundaries.

The Array is for appending.  It must always own its memory.  Therefore 
you should be able to pass it around by reference, so Array is a *class* 
and cannot be nearly as lightweight as T[].

You see, many of their properties are orthogonal.  If you drop one, you 
lose flexibility.

> Besides, Array!(T) is not a good name for build-in type.

Names are placeholders here, not an actual proposal.



More information about the Digitalmars-d mailing list