Getting a working example of opIndexAssign using opSlice ... have troubles ...

Bastiaan Veelo Bastiaan at Veelo.net
Sun Aug 15 21:15:02 UTC 2021


On Sunday, 15 August 2021 at 20:41:51 UTC, james.p.leblanc wrote:
>     struct A
>     {
>         int opIndexAssign(int v);  // overloads a[] = v
>         int opIndexAssign(int v, size_t[2] x);  // overloads 
> a[i .. j] = v
>         int[2] opSlice(size_t x, size_t y);     // overloads i 
> .. j
>     }
>
>     void test()
>     {
>         A a;
>         int v;
>
>         a[] = v;  // same as a.opIndexAssign(v);
>         a[3..4] = v;  // same as a.opIndexAssign(v, 
> a.opSlice(3,4));
>     }
>
> I have hacked at this trying to get a simple working example.

Not sure if this does enough of what you’re looking for, but this 
covers the minimal steps to get it working: 
https://run.dlang.io/is/m5svQ2

```d

import std;

struct A
{
     int opIndexAssign(int v) // overloads a[] = v
     {
         writeln(__FUNCTION__);
         return 42;
     }
     int opIndexAssign(int vh, size_t[2] x)  // overloads a[i .. 
j] = v
     {
         writeln(__FUNCTION__);
         return 43;
     }
     int[2] opSlice(size_t x, size_t y)     // overloads i .. j
     {
         writeln(__FUNCTION__);
         return [44, 45];
     }
}

void main()
{
     A a;
     int v;

     a[] = v;  // same as a.opIndexAssign(v);
     a[3..4] = v;  // same as a.opIndexAssign(v, a.opSlice(3,4));
}
```

— Bastiaan.




More information about the Digitalmars-d-learn mailing list