Java 8 gave us many fun ways to use functional interfaces and with them a new annotation: @FunctionalInterface. Its job is to tell the compiler to yell at us if we fail to stick to the rules of a functional interface (only one abstract method that needs overriding please).
There are 43 interfaces in the java.util.function package with this annotation. A search of jdk.1.8.0/src for @FunctionalInterface
only turns up 57 hits. Why are the other interfaces (such as AutoCloseable) that could have added @FunctionalInterface
still missing it?
There is a bit of a vague hint in the annotations documentation:
"An informative annotation type used to indicate that an interface type declaration is intended to be a functional interface"
Is there any good reason NOT to intend that an interface I've designed (that may simply happen to be a functional interface) not be used as one? Is leaving it off an indication of anything besides not realizing it could have been added?
Isn't adding abstract methods to any published interface going to screw anyone implementing it, functional or not? I feel cynical assuming they just didn't bother to hunt them all down but what other explanation is there?
Update: After looking over "Should 'Comparable' be a 'Functional interface'?" I find I still have nagging questions. When a Single Method Interface and a Functional Interface are structurally identical what's left to be different? Is the difference simply the names? Comparable and Comparator are close enough to the same semantically. Turns out they are different structurally though so still not the best example...
Is there a case when an SMI is structurally fine to use as a Functional Interface but still discouraged over the semantic meaning of the name of the interface and the method? Or perhaps the contract implied by the Javadocs?
Well, an annotation documenting an intention would be useless if you assume that there is always that intention given.
You named the example AutoCloseable
which is obviously not intended to be implemented as a function as there’s Runnable
which is much more convenient for a function with a ()->void
signature. It’s intended that a class implementing AutoCloseable
manages an external resource which anonymous classes implemented via lambda expression don’t do.
A clearer example is Comparable
, an interface
not only not intended to be implemented as a lambda expression, it’s impossible to implement it correctly using a lambda expression.
interface
with @FunctionalInterface
by example:interface
has programming language semantics, e.g. AutoClosable
or Iterable
(that’s unlikely to happen for your own interfaces)interface
has arbitrary implementations and/or is more an identifier than the actual implementation, e.g. java.net.ProtocolFamily
, or java.lang.reflect.GenericArrayType
(Note that the latter would also inherit a default
implementation for getTypeName()
being useless for lambda implementations as relying on toString()
)The instances of this interface
should have an identity, e.g. java.net.ProtocolFamily
, java.nio.file.WatchEvent.Modifier
, etc. Note that these are typically implemented by an enum
Another example is java.time.chrono.Era
which happens to have only a single abstract
method but its specification says “Instances of Era
may be compared using the ==
operator.”
interface
is intended to alter the behavior of an operation for which an implementation of the interface
without inheriting/implementing anything else makes no sense, e.g. java.rmi.server.Unreferenced
java.io.Closeable
, java.io.Flushable
, java.lang.Readable
java.awt
: ActiveEvent
should be implemented by an AWTEvent
, PrinterGraphics
by a Graphics
, the same applies to java.awt.print.PrinterGraphics
(hey, two interface
s for exactly the same thing…), wheras javax.print.FlavorException
should be implemented by a javax.print.PrintException
subclass@FunctionalInterface
for symmetry with other multi-method event listener that can’t be functional interfaces, but actually event listeners are good candidates for lambda expressions. If you want remove a listener at a later time, you have to store the instance but that’s not different to, e.g. inner class listener implementations.The library maintainer has a large codebase with more than 200 candidate types and not the resources to discuss for every interface
whether it should be annotated and hence focuses on the primary candidates for being used in a functional context. I’m sure, that, e.g. java.io.ObjectInputValidation
, java.lang.reflect.InvocationHandler
, juc RejectedExecutionHandler
& ThreadFactory
wouldn’t be bad as @FunctionalInterface
but I have no idea whether, e.g. java.security.spec.ECField
makes a good candidate. The more general the library is, the more likely users of the library will be able to answer that question for a particular interface
they are interested in but it would be unfair to insist on the library maintainer to answer it for all interfaces.
In this context it makes more sense to see the presence of a @FunctionalInterface
as a message that an interface
is definitely intended to be usable together with lambda expressions than to treat the absence of the annotation as an indicator for it’s being not intended to be used this way. This is exactly like the compiler handles it, you can implement every single abstract method interface
using a lambda expression, but when the annotation is present it will ensure that you can use this interface
in this way.
Planned expansion. Just because an interface matches the requirements of an SMI now doesn't mean that expansion isn't needed later.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With