Operator Overloading with multiple return types

Ali Çehreli acehreli at yahoo.com
Fri Mar 15 21:46:50 UTC 2019


On 03/15/2019 02:43 PM, Sebastiaan Koppe wrote:
> On Friday, 15 March 2019 at 21:35:12 UTC, eXodiquas wrote:
>> Is there any way to achive this behaivour with D2?
> 
> Yep. Just make the return type in the function declaration `auto`. You 
> are then free to return a different type in each static branch.

Or use template constraints:


struct Vector {
   Vector opBinary(string op)(Vector rhs)
     if (op == "+") {
       return Vector();
     }

   double opBinary(string op)(Vector rhs)
     if (op == "/") {
       return 0.5;
     }
}

Ali


More information about the Digitalmars-d-learn mailing list