opSlice() or opIndex() for the entire slice?

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Sep 4 12:19:09 PDT 2014


On Thursday, 4 September 2014 at 19:12:27 UTC, Ali Çehreli wrote:
> The documentation says "To overload a[], simply define opIndex 
> with no parameters":
>
>   http://dlang.org/operatoroverloading.html#Slice
>
> And it works with some uses of a[]. However, opSlice() seems to 
> be needed to actually use the returned slice further.
>
> Note that opSlice() also seems to be for backward 
> compatibility: "For backward compatibility, a[] and a[i..j] can 
> also be overloaded by implementing opSlice() with no arguments 
> and opSlice(i, j) with two arguments, respectively. This only 
> applies for one-dimensional slicing, and dates from when D did 
> not have full support for multidimensional arrays. This usage 
> of opSlice is discouraged."
>
> How can I achieve the last line in main() without needing 
> opSlice()? I am trying to let the Slice type handle opAssign() 
> and friends.
>

--8<-- snip --8<--

>     // This requires opSlice.
>     c[] = 42;
> }

Have you tried opIndexAssign()?


More information about the Digitalmars-d-learn mailing list