title: Groovy expando metaclass
body: <p>I've developed a Class that has some methods that augment Integer, it mainly lets me do this:</p>
  
  <pre><code>def total = 100.dollars + 50.euros
  </code></pre>
  
  <p>Now I have to extend Integer.metaClass doing something like this:</p>
  
  <pre><code>Integer.metaClass.getDollars = {-&gt;
      Money.Dollar(delegate)
  }
  </code></pre>
  
  <p>I tried putting that at the bottom of the file, before the Money class declaration, but the compiler says that a class Named Money already exists, I know why it happens (because groovy creates a class with the name of the file with an empty static void main to run this code).</p>
  
  <p>I also tried using a static block inside the class like this: </p>
  
  <pre><code>static {
      Integer.metaClass.getDollars = {-&gt;
          Money.Dollar(delegate)
      }
  }
  </code></pre>
  
  <p>This neither works.</p>
  
  <p>A third solution would be to change the file name (like MoneyClass.groovy) and keep the class name (class Money) but that seems a bit weird.</p>
  
  <p>Is there anything else I can do? Thanks.</p>