question about potentially compatible expression

Paul Sandoz paul.sandoz at oracle.com
Thu Oct 17 02:11:54 PDT 2013


Hi Jon,

For "class y<P extends Z"> we don't know what the type of P is so an instance of P cannot be created by class y. Further more, even if we could, P might not be a functional interface, for example:

    static interface ZZ extends Z {
        public void q();
        public void qq();
    }

    static class YY extends y<ZZ> { // P == ZZ
        public void doit(){
            ZZ zz = this::q; // Does this compile?
        }
    }

For "public <P extends Z> void boo(P t)" then the type of P can be known since it is localized just to the method call, and in your example P is inferred to be Z (it cannot be anything else).

Perhaps a clearer error message would help.

Paul.

On Oct 17, 2013, at 12:42 AM, "Rafkind, Jon" <jon.rafkind at hp.com> wrote:

> 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