Java 8 lambda表达式和第一类值 [英] Java 8 lambda expression and first-class values

查看:121
本文介绍了Java 8 lambda表达式和第一类值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 8闭包真的是一流的价值还是仅仅是语法糖?

Are Java 8 closures really first-class values or are they only a syntactic sugar?

推荐答案

我想说Java 8闭包(Lambdas)既不仅仅是语法糖,也不是一流的价值观。

I would say that Java 8 closures ("Lambdas") are neither mere syntactic sugar nor are they first-class values.

我在回答中解决了语法糖问题/ a>到另一个StackExchange问​​题。

I've addressed the issue of syntactic sugar in an answer to another StackExchange question.

至于lambdas是否是头等舱它真的取决于你的定义,但我会说一个lambdas不是'真的是第一堂课。

As for whether lambdas are "first class" it really depends on your definition, but I'll make a case that lambdas aren't really first class.

在某种意义上,lambda想成为一个函数,但Java 8并没有添加函数类型。相反,lambda表达式被转换为功能接口的实例。这允许将lambda添加到Java 8中,只需对Java的类型系统进行微小的更改。转换后,结果就像任何其他引用类型的引用一样。实际上,使用Lambda(例如,在传递lambda表达式作为参数的方法中)与通过接口调用方法无法区分。接收函数接口类型的参数的方法无法判断它是否传递了lambda表达式或某个类的实例恰好实现了该功能接口。

In some sense a lambda wants to be a function, but Java 8 is not adding function types. Instead, a lambda expression is converted into an instance of a functional interface. This has allowed lambdas to be added to Java 8 with only minor changes to Java's type system. After conversion, the result is a reference just like that of any other reference type. In fact, using a Lambda -- for example, in a method that was passed a lambda expression as parameter -- is indistinguishable from calling a method through an interface. A method that receives a parameter of a functional interface type can't tell whether it was passed a lambda expression or an instance of some class that happens to implement that functional interface.

有关lambda是否为对象的更多信息,请参阅 Lambda FAQ Answer 这个问题。

For more information about whether lambdas are objects, see the Lambda FAQ Answer to this question.

鉴于lambda被转换为对象,它们(字面上)继承了对象的所有特征。特别是,对象:

Given that lambdas are converted into objects, they inherit (literally) all the characteristics of objects. In particular, objects:


  • 有各种方法,如等于 getClass hashCode 通知 toString 等待

  • 有一个标识哈希码

  • 可以是锁定已同步

  • 可以使用 == 进行比较, != instanceof 运营商

  • have various methods like equals, getClass, hashCode, notify, toString, and wait
  • have an identity hash code
  • can be locked by a synchronized block
  • can be compared using the == and != and instanceof operators

等等。事实上,所有这些都与lambdas的预期用法无关。他们的行为基本上是未定义的。你可以编写一个使用其中任何一个的程序,你会得到一些结果,但结果可能因版本而异(甚至可以运行!)。

and so forth. In fact, all of these are irrelevant to the intended usage of lambdas. Their behavior is essentially undefined. You can write a program that uses any of these, and you will get some result, but the result may differ from release to release (or even run to run!).

更简洁地重申,在Java中,对象具有身份,但是值(特别是函数值,如果它们存在的话)不应该具有任何身份概念。 Java 8没有函数类型。相反,lambda表达式被转换为对象,因此它们有很多与功能无关的包袱,特别是身份。对我来说这似乎不是头等舱。

Restating this more concisely, in Java, objects have identity, but values (particularly function values, if they were to exist) should not have any notion of identity. Java 8 does not have function types. Instead, lambda expressions are converted to objects, so they have a lot baggage that's irrelevant to functions, particularly identity. That doesn't seem like "first class" to me.

更新2013-10-24

自从几个月前发布我的答案以来,我一直在思考这个话题。从技术角度来看,我上面写的所有内容都是正确的。结论可能更准确地表达为Java 8 lambdas不是(而不是 first-class )值,因为它们带有很多对象包袱。然而,仅仅因为它们不纯不意味着它们不是一流的。考虑一流功能的维基百科定义。简而言之,那里列出的考虑职能一流的标准是能够:

I've been thinking further on this topic since having posted my answer several months ago. From a technical standpoint everything I wrote above is correct. The conclusion is probably expressed more precisely as Java 8 lambdas not being pure (as opposed to first-class) values, because they carry a lot of object baggage along. However, just because they're impure doesn't mean they aren't first-class. Consider the Wikipedia definition of first-class function. Briefly, the criteria listed there for considering functions first-class are the abilities to:


  • 将函数作为其他函数的参数传递

  • 从其他函数返回函数

  • 将函数赋值给变量

  • 将函数存储在数据结构中

  • 函数是匿名的

  • pass functions as arguments to other functions
  • return functions from other functions
  • assign functions to variables
  • store functions in data structures
  • have functions be anonymous

Java 8 lambdas符合所有这些条件。所以这确实使它们看起来是一流的。

Java 8 lambdas meet all of these criteria. So that does make them seem first-class.

该文章还提到了没有特殊状态的函数名称,而函数的名称只是一个类型为函数类型的变量。 Java 8 lambdas不符合最后一个标准。 Java 8没有函数类型;它有功能接口。它们像函数类型一样有效地使用,但它们根本不是函数类型。如果你有一个类型是函数接口的引用,你不知道它是lambda,匿名内部类的实例,还是碰巧实现该接口的具体类的实例。

The article also mentions function names not having special status, instead a function's name is simply a variable whose type is a function type. Java 8 lambdas do not meet this last criterion. Java 8 doesn't have function types; it has functional interfaces. These are used effectively like function types, but they aren't function types at all. If you have a reference whose type is a functional interface, you have no idea whether it's a lambda, an instance of an anonymous inner class, or an instance of a concrete class that happens to implement that interface.

总之,Java 8 lambdas比我原先想象的更加一流。它们不是一等函数。

In summary, Java 8 lambdas are more first-class functions than I had originally thought. They just aren't pure first-class functions.

这篇关于Java 8 lambda表达式和第一类值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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