opIndexUnary post in-/decrement how to ?

Tejas notrealemail at gmail.com
Wed Jul 14 11:31:36 UTC 2021


On Wednesday, 14 July 2021 at 10:07:38 UTC, wjoe wrote:
> I'm want to do something like this
> ```D
> part_int_t!(1,2,3) i;
>
> auto x = -i[0];
> --i[1]; // 1
> i[1]++; // 2
>
> ```
> I think the operator I need to overload would be opIndexUnary 
> which I did.
> (1) compiles.
> (2) doesn't - the compiler complains that i.opIndex isn't an 
> lvalue and can't be modified.
> The language spec says that Post in- and decrement are 
> rewritten but something's fishy.
> What's going on behind the scene and how can I make it work?

Please check the language spec here:

https://dlang.org/spec/operatoroverloading.html#postincrement_postdecrement_operators

You can't directly overload the postincrement operator.

You need to rewrite it like:

``` {auto a = i[1] , ++i[1] , a} //note the , not the ;```

Sorry I can't provide something even more concrete.


More information about the Digitalmars-d-learn mailing list