Range Type

Craig Black cblack at ara.com
Mon Mar 24 07:10:47 PDT 2008


"Janice Caron" <caron800 at googlemail.com> wrote in message 
news:mailman.192.1206357352.2351.digitalmars-d at puremagic.com...
>I know this has cropped up before (in discussions about multiple
> dimension arrays), but adding a range type would also really help with
> the whole business of returning slices. (See the many other threads
> currently buzzing with this topic).
>
> A range is nothing more than a two-element struct
>
>    struct Range(T,U=T)
>    {
>        T begin;
>        U end;
>    }
>
> However, if you throw in some extra language support, it gets really,
> really useful. Basically, you want the ".." infix operator always to
> create a range. Thus
>
>    auto x = 3 .. 4;
>
> creates a Range!(int) with values { 3, 4 }. In general (a .. b) should
> evaluate to a Range!(typeof(a),typeof(b)) with values { a, b }.
> Finally, you also want [] and opSlice() to accept Range! parameters,
> so that
>
>    s = s[a..b];
>
> can always be rewritten as
>
>    auto t = a..b;
>    s = s[t];
>
> In general, opSlice(Range r) should be eqivalent to opSlice(r.begin, 
> r.end).
>
> In my opinion language support for ranges (allowing .. to return a
> range, and allowing [] to accept a range) has advantages above and
> beyond those already discussed, and may also allow many other exciting
> possibilites we haven't even thought of yet.

I've actually thought of this before.  IMO, a built-in range type is good 
fit for D  It also seems like it would be pretty simple to implement.

-Craig 





More information about the Digitalmars-d mailing list