Single Abstract Method for SAMs cannot be polymorphic

Brian Goetz brian.goetz at oracle.com
Tue Apr 2 13:23:54 PDT 2013


Not actually true.  SAM methods can be generic.  However, the only way 
to assign to such a SAM is with a method reference to a generic method. 
  There's no syntax for *lambdas* that allows you to specify the type 
params.

You should be able to use Gen::new in your example (though not sure if 
the wildcards are going to mess it up.)


On 4/2/2013 4:04 PM, Grégoire Neuville wrote:
> Hi all,
>
> The below code :
>
> public class TestGenericSAM {
>
>    class Gen<B> {}
>
>    @FunctionalInterface
>    interface CoArbitrary<A> {
>      abstract <B> Gen<B> coarbitrary(A a, Gen<B> g);
>    }
>
>    void test() {
>      CoArbitrary<?> coArb = (a, g) -> new Gen<>();
>    }
> }
>
> doesn't compile. I guess this is by design (the annotation alone is marked
> as erroneous by IntelliJ), but I'm just wondering why.
>
> Thanks a lot for any explanation !
>


More information about the lambda-dev mailing list