opIndexUnary post in-/decrement how to ?

vit vit at vit.vit
Wed Jul 14 13:09:56 UTC 2021


On Wednesday, 14 July 2021 at 12:49:58 UTC, Tejas wrote:
> On Wednesday, 14 July 2021 at 12:35:07 UTC, wjoe wrote:
>> On Wednesday, 14 July 2021 at 11:31:36 UTC, Tejas wrote:
>>>
>>> ``` {auto a = i[1] , ++i[1] , a} //note the , not the ;```
>>>
>>> Sorry I can't provide something even more concrete.
>>
>> Yes I saw that, and I suppose it would work just fine if it 
>> were rewritten to just  ```++i[1]```.
>> What I'm struggling to understand is the ```{auto a = i[1], 
>> ... ,a}``` part. I can't parse that. What's up with the 
>> assignment and the comma stuff ?
>
> I think it's a bug, because the following works:
>
> ```d
>
> import   std.stdio;
>
> struct abc{
>     int[100] a;
>     int opIndex(int index){
>         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;
>     int[100] a;
>     int temp;
>     writeln (a[20]++);
>
>     writeln(a[20]);
>
>     writeln(++s[20]);
>
>     writeln(s[20]);
>
>     //writeln(s[0]++);// doesn't work for some reason
>
>     writeln(s[0]);
>
>     writeln(s++);//but this works!!
>
>     writeln(s);
> }
>
> ```

 From doc: https://dlang.org/spec/operatoroverloading.html
Postincrement e++ and Postdecrement e-- Operators
These are not directly overloadable, but instead are rewritten in 
terms of the ++e and --e prefix operators:

Postfix Operator Rewrites
op	rewrite
e--	(auto t = e, --e, t)
e++	(auto t = e, ++e, t)


Rewriting part doesn't work with operator overloading.




More information about the Digitalmars-d-learn mailing list