opIndex() may hide opSlice()

Patrick Schluter via Digitalmars-d digitalmars-d at puremagic.com
Fri Mar 10 10:14:20 PST 2017


On Friday, 10 March 2017 at 15:41:31 UTC, Jonathan M Davis wrote:
> 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

Indexing is a bit like slicing with only 1 element. Slicing is 
the generalisation of the indexing operation. I think it's quite 
logical. This said I know nothing about the rationale and 
discussions about that subject. This was purely my wag (wild ass 
guess).


More information about the Digitalmars-d mailing list