opIndexUnary post in-/decrement how to ?

Tejas notrealemail at gmail.com
Wed Jul 14 18:27:13 UTC 2021


On Wednesday, 14 July 2021 at 17:42:03 UTC, Ali Çehreli wrote:
> On 7/14/21 9:13 AM, Tejas wrote:
>
> >      ref/*notice this ref*/ int opIndex(int
> index)return/*NOTICE THE
> > RETURN*/ {
>
> Indeed... I cover that 'ref' here:
>
>
> http://ddili.org/ders/d.en/operator_overloading.html#ix_operator_overloading.return%20type,%20operator
>
> Two quotes from that section:
>
> 1) "it is advisable to observe the return type of the same 
> operator on fundamental types."
>
> NOTE: I agree that for that to make sense, one needs to know 
> the "return type" of fundamental type operations. It may not be 
> clear that ++i is a reference to 'i' ("i itself"):
>
> void main() {
>   int i;
>   ++i = 42;
>   assert(i == 42);
> }
>
> 2) "With the exception of opAssign, it is recommended that the 
> operators that modify the object return the object itself. 
> [...] The return type is the type of the struct, marked by the 
> ref keyword to mean reference."
>
> Ali

To be honest even the compiler error should have been a dead 
giveaway that opIndex was returning a _constant_ value and the 
```++``` operator can't operate on that... I should've taken the 
diagnostic to heart and not just tried to brute force my way 
through the problem.


Obviously, I retract my statement that this might be a compiler 
bug.


More information about the Digitalmars-d-learn mailing list