应该'可比较< T>'是一个'功能界面'? [英] Should 'Comparable<T>' be a 'Functional interface'?

查看:140
本文介绍了应该'可比较< T>'是一个'功能界面'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

功能接口的定义是功能接口是一个只有一个抽象方法
的接口(除了Object的方法),因此代表一个单一的函数契约。



根据这个定义,Comparable绝对是一个功能界面。



lambda表达式的定义是一个lambda表达式就像方法:它提供了一个形式参数列表
和一个正文 - 一个表达式或块 - 用这些参数表示。



评估lambda表达式产生一个功能接口的实例。



因此,lambda表达式的目的是通过实现
来创建功能接口的实例。功能界面的单一功能。



ie。允许使用单个函数创建实例。



让我们看看Comparable,这个界面是否设计用作单个函数?
ie。它是否仅用于创建具有此单一函数的实例?



Comparable的文档以此接口对每个类的对象强制执行$ b的顺序$ b实现它。这个顺序被称为类的自然顺序,类的compareTo方法被称为
作为其自然比较方法。



上面的句子清楚地表明Comparable并不是用作单个函数,而是总是
意味着由一个类实现,它通过添加这个单个函数对其实例有自然的排序。 / p>

这意味着它不是设计为使用lambda表达式创建的。



重点是我们不会有任何仅仅是可比较的对象,它意味着要实现,因此使用
作为类的附加函数。



所以,是否有一种Java语言,creati是否阻止了Comparable的lambda表达式?
接口的设计者是否可以决定这个接口是由一个类实现的,并不是因为使用lambda表达式使用这个单一方法的
作为实例?



仅仅因为接口恰好有单个抽象方法,它不应被视为功能接口。



可能是,如果Java提供类似NotFunctional的注释,编译器可以检查此接口是否使用
来创建lambda表达式,例如。

  @NotFunctional 
public interface Comparable< T> {public int compareTo(T t); }


解决方案

lambda表达式可用于实例需要一个具有单个抽象方法的接口。你写道,


仅仅因为接口碰巧有单一的抽象方法,所以不应该把它看作是一个功能接口。


这是完全正确的。使用单个抽象方法是接口的结构属性,使其有资格使用lambda实现。但是,使用lambda实现接口是否有意义语义是否合理是一个不同的故事。后者是 @FunctionalInterface 注释的目的。当它出现在接口上时,它表示 intent 该接口对于使用lambda实现是有用的。



值得注意的是,可比较界面缺少 @FunctionalInterface 注释。



虽然它是使用lambda作为 Comparable 实现可能是荒谬的,似乎没有任何理由创建一个机制来阻止这样做。看起来这样做不会成为错误的根源,这是开发这种机制的一个很好的理由。相比之下, @FunctionalInterface 注释旨在引导程序员处于正确的方向,而不是禁止某些可能存在错误但似乎并不真正有害的东西。


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 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 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 creation of an instance with the single function.

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

The documentation of Comparable 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 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 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 craeted as an instance with this single method by use of a lambda expression?

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

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

@NotFunctional
public interface Comparable<T> { public int compareTo(T t); }

解决方案

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 a 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.

这篇关于应该'可比较&lt; T&gt;'是一个'功能界面'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆