A opIndexUnary quiz

monarch_dodra monarchdodra at gmail.com
Tue Jan 1 13:39:12 PST 2013


On Tuesday, 1 January 2013 at 19:32:10 UTC, Era Scarecrow wrote:
> On Tuesday, 1 January 2013 at 17:52:20 UTC, bearophile wrote:
>> A small quiz (it's a reprise of an older discussion). This 
>> code compiles with no errors, but do you see anything wrong 
>> here?
>
>  Well I see that you have opIndexUnary twice; According to the 
> manual you wouldn't need as it would rewrite the code so you 
> only need it once; Also they return void when they should 
> return the value of x before/after the change.
>
>  Also although the alias this makes the number increment, it 
> should be specifying 'x', not that it seems to make a 
> difference.

The notion of "right" does not really mean "on which side the 
operator is". Rather, "which operand does it apply to":

"A + B"

If A doesn't provide opBinary, then the compiler will move on to 
see if B provides opBinaryRight.

In the context of opUnary (and opIndexUnary), it makes no sense 
to talk about a "right" version. This should be caught (IMO) by 
the compiler.

Also, opUnary *can* return a void. However, in that case, the 
compiler will be unable to auto generate the post version of the 
unary operation. This is a legal design decision, taken (for 
example) by std.container.Array.


More information about the Digitalmars-d-learn mailing list