opIndexUnary post in-/decrement how to ?

Tejas notrealemail at gmail.com
Wed Jul 14 16:13:35 UTC 2021


On Wednesday, 14 July 2021 at 15:08:56 UTC, wjoe wrote:
> On Wednesday, 14 July 2021 at 14:50:01 UTC, Mike Parker wrote:
>> On Wednesday, 14 July 2021 at 12:35:07 UTC, wjoe wrote:
>>> [...]
>>
>> It's how the contract of post-inc/dec work---pre-inc/dec 
>> return the modified value, post-inc/dec return the original 
>> value.
>>
>> [...]
>
> That makes a lot of sense now, thank you!

**IT WORKS NOW**

Thanks vit for the ```ref``` idea!

```d
import   std.stdio;

struct abc{
     int[100] a;
     static int temp;
     ref/*notice this ref*/ int opIndex(int index)return/*NOTICE 
THE RETURN*/ {
         return a[index];
     }
     int opIndexUnary(string s)(int index)
         if(s == "++"){
         return ++a[index];
         }
     int[] opUnary(string s)() if (s == "++"){
         return a[] += 1;
     }
}

void main (){
     abc s;

     writeln(s[0]++);// doesn't work for some reason EDIT: IT NOW 
WORKS!!!!!!!!

     writeln(s[0]);
     writeln(++s[0]);

    // writeln(s++);//but this works!!

    // writeln(s);
}

```


More information about the Digitalmars-d-learn mailing list