Matrix-type-friendly syntax and more

Christophe travert at phare.normalesup.org
Thu Nov 3 06:15:13 PDT 2011


kenji hara , dans le message (digitalmars.D:146395), a écrit :
> I got an idea for multidimentional indexing and slicing.
> http://d.puremagic.com/issues/show_bug.cgi?id=6798
> 
> I think opSlice!n (n is dimension integer typed size_t) is rarely
> used, therefore using that name for the enhancement is almost safe for
> backward compatibility.
> 

Another option, that I must have posted somewhere else in the past:

T..T is a type similar to a Tuple with two values, and represents a 
range going from the first element to the second element excluded.

Operations are defined for T..T just like they are for arrays:

ref T front(size_t..size_t a) { assert(a[0] < a[1]; return a[0]; }
void  popFront(size_t..size_t a) { assert(a[0] < a[1]_; ++a[0]; }
bool  empty(size_t..size_t a) { return a[0] == a[1]; }
etc...

Any template that takes a range can take a slice, since front, popFront 
and empty are defined.

map!"a*2"(1..10); // instead of map!"a*2"(iota(1, 10));

foreach (a, x..y) is a normal iteration over a range.

a[1..2, 3] calls A.opIndex(int..int, int).


etc...




More information about the Digitalmars-d mailing list