About FunctionalInterface Annotation

Graham Allan grundlefleck at gmail.com
Thu Sep 19 03:18:03 PDT 2013

Hi Kaan,

In your scenario, are you not recompiling library B against a new version
of library A? I would expect a compile error when you recompile, rather
than a runtime error. I'm not sure about the picture wrt to drop-in binary

Also, this is not a new problem. If Runnable were to get a new method added
to it, all the anonymous implementations in the wild would fail to compile.
I would argue (FWIW) that library developers should have been aware their
SAM types were used in that way, they should also be aware their functional
interfaces will be used in that way.

You could argue that javac should be able to emit a warning when an
interface _not_ marked as @FunctionalInterface is used in a lambda
expression. However, that would raise a lot of warnings against existing,
pre JDK 8 libraries that declare functional interfaces, but don't have the
annotation to apply to it, e.g. Guava's Function type.


On 19 September 2013 08:40, Kaan Yamanyar <kaan at yamanyar.com> wrote:

> Hi,
> According to javadoc of FunctionalInterface the compiler will treat any
> interface meeting the definition of a functional interface as a functional
> interface regardless of whether or not a FunctionalInterface annotation is
> present on the interface declaration.
> Let's assume there are two libraries (jar) I am using as a developer : A,B.
> Library "A" has an interface without the annotation and library "B" makes
> use of this interface using it as a functional interface.
> Meanwhile I upgraded version of library "A" due to some new features I need
> and let's assume that developers of "A" added one more method to interface
> which "B" was using as functional interface.
> Now I am going to have runtime exceptions from library "B" due to fact that
> the functional interface it assumes is not a functional interface any more.
> Should we accept that all developers writing an interface knows the usage
> of "functional interface" and accept this problem?
> Wouldn't it be discouraging for developers to use method referencing?
> Or am I totally misunderstood the issue :)?
> Regards,
> Kaan Yamanyar
> --
> *Saygılarımla,*
> *Kaan Yamanyar*
> kaan at yamanyar.com
> www.yamanyar.com

More information about the lambda-dev mailing list