A DESCRIPTION OF THE REQUEST :
Adding @FunctionalInterface to an interface indicates to the compiler to perform a number of checks to ensure the interface is suitable as a functional interface
One check the compiler has, but doesn't do until you use it, is to ensure the use of generics on the method will not prevent it's use as a lambda.
JUSTIFICATION :
Using a generic on a method prevents the interface being used as a lambda however this annotation doesn't specify it should be checked.
The actual error message shows the compile has to check for this anyway, but only then you use.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The annotation should complain this will never work and give a reason as to why.
@FunctionalInterface
public interface InstanceFactory {
/**
* Create an instance of a class or return null if the factory cannot
* @param tClass to create an instance of
* @param <T> the type of the instance
* @return the instance created or null if this factory is not appropriate.
*/
<T> T create(Class<T> tClass);
}
ACTUAL -
The actual error occurs when you attempt to use this interface as a lambda.
InstanceFactory nullFactory = t -> null;
produces the error
Error:(15, 39) java: incompatible types: invalid functional descriptor for lambda expression
method <T>(java.lang.Class<T>)T in interface InstanceFactory is generic
So the compiler recognises the method signature is not suitable for a lambda but it only tells you this when you attempt to use.
AFAIC The whole point of the @FunctionalInterface is to warn you about an interface which cannot be used as a lambda when you write the interface, not when you use it.
---------- BEGIN SOURCE ----------
@FunctionalInterface
public interface InstanceFactory {
/**
* Create an instance of a class or return null if the factory cannot
* @param tClass to create an instance of
* @param <T> the type of the instance
* @return the instance created or null if this factory is not appropriate.
*/
<T> T create(Class<T> tClass);
public static void main(String[] args) {
InstanceFactory nullFactory = t -> null;
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Don't use a lambda in this case, then you won't get the error.
No way to make it produce the error for the interface.
Adding @FunctionalInterface to an interface indicates to the compiler to perform a number of checks to ensure the interface is suitable as a functional interface
One check the compiler has, but doesn't do until you use it, is to ensure the use of generics on the method will not prevent it's use as a lambda.
JUSTIFICATION :
Using a generic on a method prevents the interface being used as a lambda however this annotation doesn't specify it should be checked.
The actual error message shows the compile has to check for this anyway, but only then you use.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The annotation should complain this will never work and give a reason as to why.
@FunctionalInterface
public interface InstanceFactory {
/**
* Create an instance of a class or return null if the factory cannot
* @param tClass to create an instance of
* @param <T> the type of the instance
* @return the instance created or null if this factory is not appropriate.
*/
<T> T create(Class<T> tClass);
}
ACTUAL -
The actual error occurs when you attempt to use this interface as a lambda.
InstanceFactory nullFactory = t -> null;
produces the error
Error:(15, 39) java: incompatible types: invalid functional descriptor for lambda expression
method <T>(java.lang.Class<T>)T in interface InstanceFactory is generic
So the compiler recognises the method signature is not suitable for a lambda but it only tells you this when you attempt to use.
AFAIC The whole point of the @FunctionalInterface is to warn you about an interface which cannot be used as a lambda when you write the interface, not when you use it.
---------- BEGIN SOURCE ----------
@FunctionalInterface
public interface InstanceFactory {
/**
* Create an instance of a class or return null if the factory cannot
* @param tClass to create an instance of
* @param <T> the type of the instance
* @return the instance created or null if this factory is not appropriate.
*/
<T> T create(Class<T> tClass);
public static void main(String[] args) {
InstanceFactory nullFactory = t -> null;
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Don't use a lambda in this case, then you won't get the error.
No way to make it produce the error for the interface.