Proposal: Multidimensional opSlice solution

Don nospam at nospam.com
Mon Mar 8 06:55:53 PST 2010


Norbert Nemec wrote:
> Hi there,
> 
> in implementing multi-dimensional arrays, the current way of overloading 
> the slicing operator does not scale up.
> 
> Currently, there is opIndex working for an arbitrary number of indices, 
> but opSlice works only for one dimension. Ultimately, it should be 
> possible to allow slicing for more than one dimension, and mixing it 
> with indexing for e.g.:
>     A[4,7..8,7,2..5]
> So far, no clean solution for overloading this has been suggested.

A solution was suggested while you were away.
You don't need a new opRange operator, a simple tuple struct like:
struct Slice(T) { T from; T to; }
in std.object is enough.
Note that:
A[4, Slice(7,8), 7, Slice(2,5)]

will work with the existing compiler. So it's just a tiny syntax sugar 
issue.
And another simple possibility is to turn 7..8 into int[2][7,8].

However, Andrei argued that slicing of multidimensional arrays is so 
rarely used that syntax sugar is not necessary. Thus, it's not in D2.




More information about the Digitalmars-d mailing list