Foreach Range Statement

Oskar Linde oskar.lindeREM at OVEgmail.com
Mon Jul 23 02:46:37 PDT 2007


Reiner Pope skrev:

> To me, it isn't obvious that $==0 in your example. But I think the real 
> value of $ is in multi-dimensional arrays, because without it you would 
> get something like:
> 
>   int[,,] a = ...;
>   int[,,] my_slice = a[1..$, 1..$, 1..$];
>   int[,,] my_slice_ugly = a[1..a.length[0], 1..a.length[1], 
> 1..a.length[2]];
> 
> To support that, I would use Andrei's suggested grammar, but instead of 
> $ translating into a.length, the compiler should first try a.length(0) 
> or a.length(1), etc, where the parameter is the parameter number where 
> the $ occurs. (It's a hack, I know, but I think it's better than $ 
> generating a delegate...)

The way I have handled multidimensional slices is to make ranges 
including $ distinct types, like:

http://www.csc.kth.se/~ol/indextypes.d

All those distinct types might be overkill, but saves some unnecessary 
parameter passing and calls to .length(x).

If $ in index expressions could behave equivalent to "end" does in that 
sample, it would be great.

Having $ translate into a.length would mean range expressions containing 
$ could never become first class citizens. With the types in 
indextypes.d one can write:

auto a = range(0, end-1);
auto b = range(end-10, end);
auto c = 7;

auto B = A[a,b,c];

It would be neat to have something at least close to this with built in 
ranges.

-- 
Oskar



More information about the Digitalmars-d mailing list