Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should 'Comparable<T>' be a 'Functional interface'?

The definition of a functional interface is "A functional interface is an interface that has just one abstract method (aside from the methods of Object ), and thus represents a single function contract."

According to this definition, the Comparable<T> is definitely a functional interface.

The definition of a lambda expression is "A lambda expression is like a method: it provides a list of formal parameters and a body - an expression or block - expressed in terms of those parameters."

Evaluation of a lambda expression produces an instance of a functional interface.

Thus, the purpose of the lambda expression is to be able to create an instance of the functional interface, by implementing the single function of the functional interface. ie. to allow the creation of an instance with the single function.

Let us look at Comparable<T>, is this interface designed for use as a single function? ie. was it designed for the creation of instances with this single function only?

The documentation of Comparable<T> starts with "This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method."

The above sentence makes it clear that the Comparable<T> is not designed to be used as a single function, but is always meant to be implemented by a class, which has natural ordering for its instances, by adding this single function.

Which would mean that it is not designed to be created by using a lambda expression?

The point is that we would not have any object which is just Comparable only, it is meant to be implemented and thus used as an additional function for a class.

So, is there a way in the Java language, by which creation of a lambda expression for Comparable<T> is prevented? Can the designer of an interface decide that this interface is meant to be implemented by a class and not meant to be created as an instance with this single method by use of a lambda expression?

Simply because an interface happens to have a single abstract method, it should not be considered as a functional interface.

Maybe, if Java provides an annotation like NotFunctional, it can be checked by the compiler that this interface is not used for the creation of a lambda expression, eg.

@NotFunctional public interface Comparable<T> { public int compareTo(T t); } 
like image 375
user2363727 Avatar asked Aug 09 '14 20:08

user2363727


People also ask

Is comparable interface functional interface?

Q #1) Is comparable a functional interface? Answer: Yes, comparable is a functional interface. It declares a single abstract method, compareTo ().

Does Comparator is a functional interface?

Yes, Comparator is a functional interface. The equals is an abstract method overriding one of the public methods of java.

When would you use a functional interface?

Functional interfaces are interfaces that ensure that they include precisely only one abstract method. Functional interfaces are used and executed by representing the interface with an annotation called @FunctionalInterface. As described earlier, functional interfaces can contain only one abstract method.

Is cloneable a functional interface?

An interface which has Single Abstract Method can be called as Functional Interface. Runnable, Comparator,Cloneable are some of the examples for Functional Interface.


1 Answers

A lambda expression can be used where an instance of an interface with a single abstract method is required. You wrote,

Simply because an interface happens to have single abstract method, it should not be considered as a functional interface.

This is exactly correct. Having a single abstract method is a structural property of an interface, one that makes it eligible to be implemented with a lambda. However, whether an interface makes sense or is semantically sensible to be implemented with lambda is a different story. The latter is the purpose of the @FunctionalInterface annotation. When it is present on an interface, it indicates the intent that the interface is useful to be implemented with a lambda.

Notably, the Comparable interface lacks the @FunctionalInterface annotation.

While it's probably nonsensical to use a lambda as a Comparable implementation, there doesn't seem to be any reason to create a mechanism to prevent this from being done. It doesn't seem like doing this would be a source of error, which would be a good reason to develop such a mechanism. By contrast, the @FunctionalInterface annotation is intended to guide programmers in the right direction instead of prohibiting something that is arguably wrong but doesn't seem truly harmful.

like image 108
Stuart Marks Avatar answered Oct 03 '22 12:10

Stuart Marks