opIndexAssign

Salih Dincer salihdb at hotmail.com
Mon Oct 2 22:11:18 UTC 2023


Hi,

opIndexAssign, which is void, cannot compromise with opIndex, 
which is a ref!  Solution: Using opSliceAssign.  Could this be a 
bug?  Because there is no problem in older versions (e.g. 
v2.0.83).

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

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

   auto opSliceAssign/*
   auto opIndexAssign//*/
   (int value) => i[] = value;
}

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

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

   s[1] = 42;
   assert(s.i == [2, 42]);
}
```
**Source:** https://run.dlang.io/is/G3iBEw
If you converted comment line 7 with // and you will see this 
error:

> onlineapp.d(19): Error: function `onlineapp.S.opIndexAssign(int 
> value)` is not callable using argument types `(int, int)`
> onlineapp.d(19):        expected 1 argument(s), not 2

SDB at 79


More information about the Digitalmars-d-learn mailing list