"+=" (overloads) with custom array slices on both lhs, and rhs ??

jfondren julian.fondren at gmail.com
Sun Sep 5 21:25:06 UTC 2021


On Sunday, 5 September 2021 at 20:49:08 UTC, james.p.leblanc 
wrote:
> On Sunday, 5 September 2021 at 20:38:29 UTC, Paul Backus wrote:
>> Please post the source code for `myarray_mod` so that we can 
>> reproduce the errors you're seeing.
>
> Hello Paul,
>
> Thanks for having a look ...
>
> James

Here's a reduction of your myArray.d that works with your 
unchanged usage code:

```d
module myArray;
import std.stdio;

struct myArray {
     int* ptr;
     size_t length;

     myArray opIndex() {
         return this;
     }

     int opIndex(size_t i) {
         return ptr[i];
     }

     void opIndexOpAssign(string op)(int val, int ind) {
         if ((op == "+") || (op == "-") || (op == "*") || (op == 
"/")) {
             mixin(" ptr[ind] " ~ op ~ "= val;");
         }
     }

     myArray opSlice(size_t start, size_t end) {
         return myArray(&ptr[start], end - start);
     }

     void opOpAssign(string op)(int rhs) if (op == "+") {
         foreach (i; 0 .. length) {
             mixin("ptr[i] " ~ op ~ "= i;");
         }
     }

     void opOpAssign(string op)(myArray rhs) if (op == "+") {
         foreach (i; 0 .. length) {
             mixin("ptr[i] " ~ op ~ "= rhs.ptr[i];");
         }
     }
}
```


More information about the Digitalmars-d-learn mailing list