Problem with method references

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Tue Jan 4 06:19:00 PST 2011


On 03/01/11 10:10, Tomasz Kowalczewski wrote:
> package org.tkowalcz.lambda.extractor;
>
> public class LambdaInConstructorExample {
>
>    public static void main( final String[] args ) {
>      // Compiles fine
>      Extractor x = LambdaInConstructorExample#getArgCount;
>    }
>
>    public LambdaInConstructorExample() {
>      // Error
>      Extractor x = LambdaInConstructorExample#getArgCount;
>    }
>
>    public int getArgCount() {
>      return 1;
>    }
> }
There is no bug here. The first method reference compiles because a 
non-static method is being referenced from a static context - in such 
cases, the following expansion is automagically applied by the compiler:

If f is an instance method of Z whose signature is A,B->C, then #obj.f 
is of type A,B->C and #Z.f is of type Z,A,B->C.

Which means that, in the first case, the compiler is looking for a 
signature of the kind (LambdaInConstructorExample)->int.

Since the second method reference occurs in a non-static context, no 
automatic expansion is applied, which means the compiler will look for a 
signature of the kind ()->int.

Now, depending on how you declare the target method in the SAM type 
Extractor, you will not be able to get both method references to 
compile, as the compiler will be looking for different signatures. I 
guess that, looking at your output, your SAM type declaration looks like 
the following:

interface Extractor {
int getX(LambdaInConstructorExample arg);
}

Maurizio



More information about the lambda-dev mailing list