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

Salih Dincer salihdb at hotmail.com
Mon Oct 2 20:34:11 UTC 2023


On Monday, 2 October 2023 at 16:05:39 UTC, Paul Backus wrote:
>
> `T[] opSlice()` is the D1 version and exists only for backwards 
> compatibility. You should use `T[] opIndex()` in new code.


Forgive me for asking again, I think opSliceAssign(T value) has 
also been improved, right?
```d
    // ...

    auto opSlice()
    {
      return elements[];
    }

    auto opSliceAssign(T value)
    {
      foreach(i; 0 .. length)
      {
        ptr[i] = value;
      }
    }
  }
```
In an old version (for example, v2.0.83), the code you 
implemented in the places where Slice is written above works as 
desired.  In the most current versions, the parameterized 
opIndexAssign(T value) gives the error:

> onlineapp.d(51): Error: function 
> `onlineapp.Matrix!double.Matrix.opIndexAssign(double value)` is 
> not callable using argument types `(double, ulong)`
> onlineapp.d(51):        expected 1 argument(s), not 2

**Source:** https://run.dlang.io/is/TPAg5m

Thanks...

SDB at 79


More information about the Digitalmars-d-learn mailing list