The difference between T[] opIndex() and T[] opSlice()

Salih Dincer salihdb at hotmail.com
Tue Oct 3 18:29:49 UTC 2023


On Tuesday, 3 October 2023 at 18:09:55 UTC, Imperatorn wrote:
>> At the very least, the spec should do a better job of 
>> documenting when the compiler will try a fallback and when it 
>> won't.
>
> Who will be the hero and add the documentation? 😇


More importantly, is there a priority order?  Because in our last 
example, when we leave a single overload, all features are 
executed through the ref opIndex except the bit:

```d
struct S
{
   int[] i;

   ref opIndex(size_t index) => i[index];

   //void opIndexAssign(int value) { i[] = value; }
   //void opIndexAssign(int value, size_t idx) { i[idx] = value; }
}

void main()
{
   auto s = S([2, 2]);

   s[0] = 2;
   assert(s.i == [2, 2]);

   s[1] = 42;
   assert(s.i == [2, 42]);

   s[0]++;
   assert(s.i == [3, 42]);

   s[0] += 1;
   assert(s.i == [4, 42]);
}
```

So the important thing is: Who is dominant when another overload 
comes into play?

SDB at 79


More information about the Digitalmars-d-learn mailing list