On 03/15/2019 03:48 PM, H. S. Teoh wrote:
On Fri, Mar 15, 2019 at 10:30:41PM +0000, eXodiquas via Digitalmars-d-learn 
wrote:
On Friday, 15 March 2019 at 21:46:50 UTC, Ali Çehreli wrote:
[...]
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

Thanks for the quick and simple answers, but I don't get this one. If
I do it that way the compiler doesn't know which function to call, or
am I doing something wrong?

Vector2 opBinary(string op)(Vector2 rhs) {
         if (op == "+") {
             return Vector2(this.x + rhs.x, this.y + rhs.y);
         } else if (op == "-") {
             return Vector2(this.x - rhs.x, this.y - rhs.y);
         }
     }

     float opBinary(string op)(Vector2 rhs) {
         if (op == "*") {
             return this.x * rhs.x + this.y * rhs.y;
         }
     }

This gives me the error:
overloads (Vector2 rhs) and (Vector2 rhs) both match argument list for
opBinary
[...]

Ali's example was unfortunately deceptively formatted.

My editor did that. :)

On my work computer, I've been experimenting with pulling the 'if', 'in', etc to the same level as the function signature:

  int foo(T)(T t)
  if (isBlah!T)
  in (!t.empty) {
    // ...
  }

Ali

Reply via email to