Bikeshed: what do we call the distinguished method of a SAM?

Brian Goetz brian.goetz at oracle.com
Wed Jul 3 06:10:27 PDT 2013


No, this is all needless panic.  Nothing has changed at all.

The notion of "functional interface" has been around from day one.  This 
is a structural concept; a functional interface (nee SAM) is one with 
one non-Object abstract method.  Functional interfaces have always been 
how lambdas get their types.

The @FunctionalInterface annotation is purely there to (a) capture and 
document design intent that this interface was intended for use as a 
lambda target and (b) engage the compiler to produce additional warnings 
when the design intent is violated.

> Hmm, until now I had accepted the introduction of the
> '@FunctionalInterface' as something that mysteriously mattered to you
> 'Functional Experts', but that was equally gratuitous and harmless ;-)
>
> But now, I'm getting worried. Is the language spec being changed again?

No.

> Will only Functional interfaces be allowed as the target types for
> lambda-conversions?

Functional interface is purely structural; something is a functional 
interface because of the methods it has, not any special syntax, 
decoration, or annotation.  Use of @FunctionalInterface is purely 
optional, but still useful.

> Why do you explicitly stress that '...Functional interfaces provide
> target types for lambda expressions and method references'?

Because that's why we gave them a name!

> Why do you seeminlgy restrict lambda conversions by stating that
> '...Functional interfaces can provide a target type in multiple
> contexts, such as assignment context, method invocation, or cast context'?
>
> Are Java programmers discouraged to use lambda expressions in places
> that expect java.lang.Runnables or java.awt.event.ActionListeners?

No, Runnable is a functional interface too.

> If so, that would be a shame. If not, this entire distinction between
> 'Functional' and non-functional SAMs looks totally deliberate to me!

The only difference is in the mind of the designer.  Some SAMs were 
designed to be stand-ins for functions; some are just accidentally so. 
@FunctionalInterface gives designers a means for capturing that for 
documentation purposes, but the compiler doesn't care whether something 
is @FunctionalInterface at all.  Only whether it is truly a functional 
interface.




More information about the lambda-libs-spec-observers mailing list