Operator overloading through UFCS doesn't work

Tommi tommitissari at hotmail.com
Sat Oct 13 10:01:26 PDT 2012


Another way to describe my reasoning...

According to TDPL, if var is a variable of a user-defined type, 
then:
++var
gets rewritten as:
var.opUnary!"++"()

Thus, it would be very logical to assume that it doesn't matter 
whether you write:
++var
...or, write the following instead:
var.opUnary!"++"()
...because the second form is what the first form gets written to 
anyway.

But, that "very logical assumption" turns out to be wrong. 
Because in D, as it stands currently, it *does* make a difference 
whether you write it using the first form or the second:

struct S
{
     int _value;
}

ref S opUnary(string op : "++")(ref S s)
{
     ++s._value;
     return s;
}

Now, writing the following compiles and works:
S var;
var.opUnary!"++"();

...while the following doesn't compile:
S var;
++var;

This behavior of the language is not logical. And I don't think 
that logic is a matter of preference or taste.


More information about the Digitalmars-d-learn mailing list