www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - [Idea] Range type

reply Vathix <chris dprogramming.com> writes:
The .. operator could return a Range type:

struct Range
{
    size_t start, end;
}


opSlice() could then be removed in favor of opIndex(Range) and other  
functions could take range parameters.


void select(HWND hwEditBox, Range sel)
{
    // ...
}

select(myhwnd, 3 .. 9);


Might want to name it Slice or something instead, since Range is pretty  
popular (and in DTL?).
Jul 23 2005
next sibling parent "Andrew Fedoniouk" <news terrainformatica.com> writes:
"Vathix" <chris dprogramming.com> wrote in message 
news:op.sudzzol2l2lsvj esi...
 The .. operator could return a Range type:

 struct Range
 {
    size_t start, end;
 }


 opSlice() could then be removed in favor of opIndex(Range) and other 
 functions could take range parameters.


 void select(HWND hwEditBox, Range sel)
 {
    // ...
 }

 select(myhwnd, 3 .. 9);


 Might want to name it Slice or something instead, since Range is pretty 
 popular (and in DTL?).
FYI: Here is the range as it is declared in Harmonia. harmonia.gx.geometry.d /** * diapason * please pay attention that both l and h belong to the range - [l,h] */ struct range { int l,h; // low and high static range opCall( int low, int high ) { range r; r.l = low; r.h = high; return r; } static range opCall( int lowandhigh ) { range r; r.l = lowandhigh; r.h = lowandhigh; return r; } bool isEmpty() { return (l > h); } int length() { return (l <= h)? h - l + 1 : 0; } bool overlap(range r) { return (max!(int)(l,r.l)) <= (min!(int)(h,r.h)); } bool opEquals(range b) { return (l == b.l) && (h == b.h); } // check if 'i' is inside the range bool contains(int i) { return (i >= l) && (i <= h); } // intersection of two ranges r3 = r1 & r2; range opAnd(range r) { return opCall( max!(int)(l,r.l), min!(int)(h,r.h)); } // union of two ranges r3 = r1 | r2; range opOr(range r) { return opCall( min!(int)(l,r.l), max!(int)(h,r.h)); } range opAddAssign(int i) { return l += i, h += i, *this; } range opSubAssign(int i) { return l -= i, h -= i, *this; } // inplace intersection r1 &= r2; range opAndAssign(range a) { return l = max!(int)(l,a.l), h = min!(int)(h,a.h), *this; } // inplace union r1 |= r2; range opOrAssign(range b) { if(isEmpty()) *this = b; else if(!b.isEmpty()) { l = min!(int)(l,b.l); h = max!(int)(h,b.h); } return *this; } static range empty() { range r; r.l = 0; r.h = -1; return r; } };
Jul 23 2005
prev sibling parent Manfred Nowak <svv1999 hotmail.com> writes:
Vathix <chris dprogramming.com> wrote:

 The .. operator could return a Range type:
[...] A type without operations is pretty useless. And as your implementation suggestion shows, it wouldn't be able to support concatenation. If thhis is true, then your idea seems to be nothing more than syntactic sugar. -manfred
Jul 23 2005