• nobody (19/19) Aug 18 2006 Overloading Slicing a[] and a[i .. j]
nobody <nobody mailinator.com> writes:
```Overloading Slicing a[] and a[i .. j]

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
Stewart Gordon <smjg_1998 yahoo.com> writes:
```nobody wrote:

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