title: Groovy mixed typing
body: <p>I see that as of Groovy 2.0 there is the possibility to add a <code>TypeChecked</code> annotation to classes or methods to trigger the <a href="http://www.infoq.com/articles/new-groovy-20" rel="nofollow">optional static checking</a>.</p>
  
  <p>I must admit that I am confused by how such a thing could work. The article gives simple examples such as</p>
  
  <pre><code>@TypeChecked
  Date test() {
      // compilation error:
      // cannot assign value of Date 
      // to variable of type int
      int object = new Date()
  
      String[] letters = ['a', 'b', 'c']
      // compilation error:
      // cannot assign value of type String 
      // to variable of type Date
      Date aDateVariable = letters[0]
  
      // compilation error:
      // cannot return value of type String 
      // on method returning type Date
      return "today"
  }
  </code></pre>
  
  <p>In this case it is clear that some checks will fail. But in the general case, one will use, say, the return value of method <code>A</code>, that is not type checked, inside the type checked method <code>B</code>. In this case I do not see how the compiler can figure out whether method <code>B</code> types are consistent, since it does not have enough information on the return value of method <code>A</code>.</p>
  
  <blockquote>
    <p>How can one enable type checking on a subset of the code without losing type safety in general?</p>
  </blockquote>
  
  <p><strong>EDIT</strong></p>
  
  <p>I try to make an example. What if I have an old class</p>
  
  <pre><code>class Old {
    public getFoo() {
      return 1
    }
  }
  </code></pre>
  
  <p>and try to use it from type-checked code, like</p>
  
  <pre><code>@TypeChecked
  class New {
    int doubleFoo() {
      return 2 * (new Old().foo)
    }
  }
  </code></pre>
  
  <p>The compiler just does not know what to do, and I guess it will fail to compile (I do not have Groovy2 installed here at work to check). But if this is the case, it become an issue to use any code written before Groovy2. So I imagine something more sophisticated is done, but I am not sure what.</p>