title: How do I modify an existing Java class to overload an operator in Groovy?
body: <p>I'm implementing a few math classes and for the first time finding problems with Java (cumbersome and duplicated code) that I really can't overcome by cleaver coding.  Operator overloading would be REALLY handy, and there are a few other things that Groovy can do a lot better (I may also try Scala at some point).</p>
  
  <p>So I'm still writing the primary classes (Right now Complex and Matrix) in Java, but I'm going to use them from Groovy.</p>
  
  <p>I changed the names of some methods (.add) to take advantage of operator overloading (.plus).</p>
  
  <p>Now, my classes interact very well with the "Number" base class, so I can use:</p>
  
  <pre><code>complex.add(number); //or 
  complex = complex + number;
  </code></pre>
  
  <p>and get an awesome answer, but I cannot use:</p>
  
  <pre><code>complex = number + complex
  </code></pre>
  
  <p>since number doesn't understand .add(Number) and even if it did, it wouldn't know how to handle the Number if it turned out to be a complex.</p>
  
  <p>Can I write a number.plus(complex) and add it to the number interface?  The code would actually be pretty easy, I could just reverse it:</p>
  
  <pre><code>Number plus(complex) {
       return complex.plus(this);
  }
  </code></pre>
  
  <p>This would work for everything of type number, but I don't know if Groovy can graft this to the number interface after the fact.  (Also, Number and Complex are in Java, so I kind of see this as an add-in for groovy).</p>
  
  <p>Any other advice?  Should I just give in and learn Scala?</p>
  
  <p>Oh, I realize that there are probably packages out there that can do this already, but part of it is just learning the math, I'm working through a quantum computing text and am implementing the exercises as I go.</p>