question about potentially compatible expression

Rafkind, Jon jon.rafkind at hp.com
Wed Oct 16 15:42:41 PDT 2013


In section 15.12.2.1 in JSR 335 is the following text

  A lambda expression, a method reference, or a constructor reference is potentially compatible with a type variable if the type variable is a type parameter of the candidate method.

Why is there a restriction that the type parameter must be on the candidate method? This rules out the possibility of using a type parameter on the enclosing class as in the following code:

public class y<P extends Z>{
   public void boo(P t){
   }

   public void q(){
   }

   public void doit(){
       boo(this::q);
   }
}

interface Z{
   public void q();
}

I get this error:

$ javac8 y.java
y.java:9: error: incompatible types: P is not a functional interface
       boo(this::q);

But attaching 'P' to method 'boo' directly works.

public <P extends Z> void boo(P t){
}


More information about the lambda-dev mailing list