www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Question about overloading opSlice(3,4)

reply nobody <nobody mailinator.com> writes:
Overloading Slicing a[] and a[i .. j]
http://digitalmars.com/d/operatoroverloading.html

Suppse I have the following:

   struct ByteArray // just bytes
   {
     byte[] b;
   }

Now suppose I want to (not want, must) overload opSlice. Now I have to deal
with 
things like:

   ByteArray bArr;
   short[]   sArr; // assume it holds data
   int[]     iArr; // assume it holds data

    bArr = sArr[];       // bArr.opSlice()
    bArr = iArr[];       // bArr.opSlice()
    bArr = sArr[0 .. 1]; // bArr.opSlice(0,2)
    bArr = iArr[0 .. 1]; // bArr.opSlice(0,4)

As I understand opSlice bArr should point to data inside either sArr or iArr 
after any of these slices. But bArr is called via opSlice() or opSlice(i,j).
So how does one do that if one gets no information about sArr or iArr?
Aug 18 2006
parent Stewart Gordon <smjg_1998 yahoo.com> writes:
nobody wrote:
 Overloading Slicing a[] and a[i .. j]
 http://digitalmars.com/d/operatoroverloading.html
 
 Suppse I have the following:
 
   struct ByteArray // just bytes
   {
     byte[] b;
   }
 
 Now suppose I want to (not want, must) overload opSlice. Now I have to 
 deal with things like:
 
   ByteArray bArr;
   short[]   sArr; // assume it holds data
   int[]     iArr; // assume it holds data
 
    bArr = sArr[];       // bArr.opSlice()
    bArr = iArr[];       // bArr.opSlice()
    bArr = sArr[0 .. 1]; // bArr.opSlice(0,2)
    bArr = iArr[0 .. 1]; // bArr.opSlice(0,4)
No such thing as bArr.opSlice factors into your code.
 As I understand opSlice bArr should point to data inside either sArr or 
 iArr after any of these slices. But bArr is called via opSlice() or 
 opSlice(i,j).
 So how does one do that if one gets no information about sArr or iArr?
What do you mean by "bArr is called"? You appear to be trying to overload the assignment operator. D doesn't let you do this, for good reasons. However, you can enable stuff like bArr[] = sArr[0 .. 1]; bArr[0..4] = iArr[0 .. 1]; by defining bArr.opSliceAssign. Stewart.
Aug 18 2006