opIndexUnary post in-/decrement how to ?

Ali Çehreli acehreli at yahoo.com
Wed Jul 14 17:42:03 UTC 2021


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



More information about the Digitalmars-d-learn mailing list