title: Lambda Expression and generic defined only in method
body: <p>Suppose I've a generic interface:</p>
  
  <pre><code>interface MyComparable&lt;T extends Comparable&lt;T&gt;&gt;  {
      public int compare(T obj1, T obj2);
  }
  </code></pre>
  
  <p>And a method <code>sort</code>:</p>
  
  <pre><code>public static &lt;T extends Comparable&lt;T&gt;&gt; 
         void sort(List&lt;T&gt; list, MyComparable&lt;T&gt; comp) {
      // sort the list
  }
  </code></pre>
  
  <p>I can invoke this method and pass a lambda expression as argument:</p>
  
  <pre><code>List&lt;String&gt; list = Arrays.asList("a", "b", "c");
  sort(list, (a, b) -&gt; a.compareTo(b));
  </code></pre>
  
  <p>That will work fine. </p>
  
  <p>But now if I make the interface non-generic, and the method generic:</p>
  
  <pre><code>interface MyComparable {
      public &lt;T extends Comparable&lt;T&gt;&gt; int compare(T obj1, T obj2);
  }
  
  public static &lt;T extends Comparable&lt;T&gt;&gt; 
         void sort(List&lt;T&gt; list, MyComparable comp) {
  }
  </code></pre>
  
  <p>And then invoke this like:</p>
  
  <pre><code>List&lt;String&gt; list = Arrays.asList("a", "b", "c");
  sort(list, (a, b) -&gt; a.compareTo(b));
  </code></pre>
  
  <p>It doesn't compile. It shows error at lambda expression saying: </p>
  
  <blockquote>
    <p>"Target method is generic"</p>
  </blockquote>
  
  <p>OK, when I compiled it using <code>javac</code>, it shows following error:</p>
  
  <pre><code>SO.java:20: error: incompatible types: cannot infer type-variable(s) T#1
          sort(list, (a, b) -&gt; a.compareTo(b));
              ^
      (argument mismatch; invalid functional descriptor for lambda expression
        method &lt;T#2&gt;(T#2,T#2)int in interface MyComparable is generic)
    where T#1,T#2 are type-variables:
      T#1 extends Comparable&lt;T#1&gt; declared in method &lt;T#1&gt;sort(List&lt;T#1&gt;,MyComparable)
      T#2 extends Comparable&lt;T#2&gt; declared in method &lt;T#2&gt;compare(T#2,T#2)
  1 error
  </code></pre>
  
  <p>From this error message, it seems like compiler is not able to infer the type arguments. Is that the case? If yes, then why is it happening like this?</p>
  
  <p>I tried various ways, searched through the internet. Then I found <a href="http://www.javacodegeeks.com/2012/02/whats-cooking-in-java-8-project-lambda.html">this JavaCodeGeeks article</a>, which shows a way, so I tried:</p>
  
  <pre><code>sort(list, &lt;T extends Comparable&lt;T&gt;&gt;(a, b) -&gt; a.compareTo(b));
  </code></pre>
  
  <p>which again doesn't work, contrary to what that article claims that it works. Might be possible that it used to work in some initial builds.</p>
  
  <p>So my question is: Is there any way to create lambda expression for a generic method? I can do this using a method reference though, by creating a method:</p>
  
  <pre><code>public static &lt;T extends Comparable&lt;T&gt;&gt; int compare(T obj1, T obj2) {
      return obj1.compareTo(obj2);
  }
  </code></pre>
  
  <p>in some class say <code>SO</code>, and pass it as:</p>
  
  <pre><code>sort(list, SO::compare);
  </code></pre>