Yippie-yeah! My favorite topic is being mentioned! I have to give a comment... :-)

On 26/08/10 22:38, bearophile wrote:
> [...]
So far in D code I have had need of (and I have seen some people need) some 
other kinds of arrays, not (well) supported by D:
C) nD rectangular dynamic arrays;
[...]
The (C) arrays are not the same thing as dynamic arrays of dynamic arrays 
because:
- Some algorithms are not designed for a triangle where rows may differ in 
length. Testing that rows are all the same length at runtime wastes time, and 
if you don't test it then it may cause bugs;
- Sometimes you want to reshape a 2D matrix, so it's useful to store the matrix 
in contiguous memory;
- Sometimes you need complex slicing, for example removal of some columns and 
rows from a 2D matrix.

Recently in D.learn we have discussed this a bit.

In C# there are both arrays of arrays as in D, and rectangular dynamic arrays, 
that are manages with a syntax similar to Pascal arrays: arr[x,y,z]. I think 
there is no so need to put them into D. But I think it's important to have a 
official type of them in Phobos, for example implemented as a struct (only the 
item type and number of dimensions are known at compile time):

RectangularArray!(T, int ndimensions)

I still have my doubt that rectangular arrays can be implemented to full power in the library. Currently, there is a number of essential languages missing to allow a sane syntax:

* ranges 'A..B' as standalone expression
* following from this: replacement of opSlice by opIndex overloaded for builtin type of range expression
* extension of range expression by stride (e.g. 'A..B:C' )
* handling of opDollar '$' for multiple dimensions

Beyond this, there are a few issues where I don't see that a library-based solution will ever have the full power of a builtin language feature:

* array expressions with clean and easy to use syntax
* syntax of type declarations: double[A,B] for static arrays and something like double[[3]] or double[:,:] for dynamic arrays are just far more straightforward and readable than any template-based syntax I could imagine.

Ultimately, I think that rectangular arrays are as fundamental of numerical computing as hash-maps and strings for other areas. Moreover, there is a huge number of physicists working with Fortran95 simply because it is about as simple to use as Basic, without classes, let alone templates. The mere thought of having to touch classes or templates would deter many of them to consider D an option.

Reply via email to