Automatic translation of opUnary!"++" into opOpAssign!"+"

Francesco Cattoglio francesco.cattoglio at gmail.com
Sat Dec 28 07:37:04 PST 2013


So, while I was studying the apropriate template constraints for 
my shiny new iota implementation, I found out this funny thing:

import std.stdio;

class Test{
     int x = 41;
     Test opOpAssign(string op)(int rhs) if (op == "+") {
          x += rhs;
          return this;
     }
}

void main() {
     Test t1 = new Test;
     //class Test has no opUnary defined, so the following
     //gets automagically converted into (t1) += (1)
     ++t1;
     writeln(t1.x); //prints 42, correct!
}

This actually comes really handy, but I couldn't find it into the 
language documentation on dlang.org, so it surprised me.
Did I miss it in the language specification? Should we add it 
somewhere to the docs?
Anyone with some spare time care to explain briefly what was the 
rationale behind this?


More information about the Digitalmars-d-learn mailing list