opIndexUnary post in-/decrement how to ?

wjoe invalid at example.com
Thu Jul 15 11:02:17 UTC 2021


On Thursday, 15 July 2021 at 04:07:49 UTC, Tejas wrote:
> Your code
> ```d
> auto x = i[1]++;
> ```
> Expands to:
> ```d
> auto x = (auto e = i[1]/*notice opIndex*/, ++i[1]/* notice 
> opIndexUnary*/, return e;);
> ```
>
> This doesn't happen with pre increment. No compiler shenanigans.

Interesting to see it spelt out like this (your remarks are very 
enlightening) so I just went one step further and rewrote this 
line like so:
```D
i[1] = 3;
auto x = (){auto e = i[1]; ++i[1]; return e;}();
assert (i[1] == 4 && x == 3);
```
This just works. But I don't think this is what happens. What I 
think happens is that the compiler rewrites ```i[1]++`` to 
something like this:
```D
i.opIndex(1).opUnary!"++"();
```
plus all the other shenanigans.

I did indeed override opIndex() but since i need to apply a bit 
mask and do some shifting I can't return anything by ref.


More information about the Digitalmars-d-learn mailing list