title: Java Comparing Generics with Comparable<? super T> from another class
body: <p>I have a "Schema" and "Field" model in which a Field represents a data type and has methods on how to parse it, and the schema is a collection of fields.  I am trying to implement generic comparison, however, I can't get the code to compile and I can't figure out the proper generic scopes. How can I get this to work?</p>
  
  <pre class="lang-java prettyprint-override"><code>class Field&lt;T extends Comparable&lt;? super T&gt;&gt; {
  
    T parse(String val) {
    ... 
    }
  }
  
  public class Schema {
    Map&lt;Integer, Field&lt;?&gt;&gt; fields;
  
    Field&lt;?&gt; getField(int index){ ... }
  }
  
  public class Comparison {
    public static &lt;T extends Comparable&lt;? super T&gt;&gt; boolean greaterThan(Field&lt;T&gt; f, String val1, String val2) {
     // compiles as expected
      return f.parse(val1).compareTo(f.parse(val2)) &gt; 0;
    }
  
    public static boolean greaterThan2(Field&lt;?&gt; f, String val1, String val2) {
     // does not compile -&gt; required capture of ? super capture of ?, provided capture of ? 
     return f.parse(val2).compareTo(f.parse(val2));
    }
  }
  
  public class App {
  
   public static void main(String[] args) {
    Schema s = ...
    // does not compile, required type Field&lt;T&gt;, provided type Field&lt;capture of ?&gt;
    Comparison.greaterThan(s.getField(0), "val1", "val2");
    // compiles
    Comparison.greaterThan2(s.getField(0), "val1","val2");
  }
  }
  
  </code></pre>