opIndex() may hide opSlice()

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Fri Mar 10 07:41:31 PST 2017


On Friday, March 10, 2017 14:15:45 Nick Treleaven via Digitalmars-d wrote:
> On Friday, 10 March 2017 at 01:10:21 UTC, H. S. Teoh wrote:
> > On Fri, Mar 10, 2017 at 01:07:33AM +0000, XavierAP via
> >
> > Digitalmars-d wrote:
> >> The web reference tersely says under its *Slice* Operator
> >> Overloading chapter [1]: "To overload a[], simply define
> >> opIndex with no parameters."
> >>
> >> Should not the overload of opSlice() with no arguments be
> >> deprecated?
> >> Am I missing something?
> >
> > Using opSlice() for slicing (i.e., arr[]) is old,
> > backward-compatible
> > behaviour.
>
> This seems non-intuitive to me (at least for single dimension
> containers) - when you see var[], do you think var is being
> indexed or do you think var is being sliced like an array
> (equivalent to var[0..$])?

Yeah, I've never understood how it made any sense for opIndex to be used for
slicing, and I've never used it that way. I generally forget that that
change was even made precisely because it makes no sense to me, whereas
using opSlice for slicing makes perfect sense. I always use opIndex for
indexing and opSlice for slicing just like they were originally designed.

- Jonathan M Davis



More information about the Digitalmars-d mailing list