Java需要关闭吗? [英] Does Java need closures?

查看:141
本文介绍了Java需要关闭吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在阅读下一个Java版本,可能支持关闭。我觉得我已经非常牢固地掌握了什么是闭包,但我不能想到一个坚实的例子,他们如何使面向对象的语言更好。

I've been reading a lot lately about the next release of Java possibly supporting closures. I feel like I have a pretty firm grasp on what closures are, but I can't think of a solid example of how they would make an Object-Oriented language "better". Can anyone give me a specific use-case where a closure would be needed (or even preferred)?

推荐答案

作为一个Lisp程序员我希望Java社区理解以下区别:函数作为对象与闭包

As a Lisp programmer I would wish that the Java community understands the following difference: functions as objects vs. closures.

a)函数可以命名或匿名。但他们也可以是自己的对象。这允许函数作为参数传递,从函数返回或存储在数据结构中。这意味着函数是编程语言中的第一类对象。

a) functions can be named or anonymous. But they can also be objects of themselves. This allows functions to be passed around as arguments, returned from functions or stored in data structures. This means that functions are first class objects in a programming language.

匿名函数不会增加语言,它们只是允许你以更短的方式写函数

Anonymous functions don't add much to the language, they just allow you to write functions in a shorter way.

b)闭包是一个函数加上绑定环境。关闭可以向下传递(作为参数)或返回向上(作为返回值)。

b) A closure is a function plus a binding environment. Closures can be passed downwards (as parameters) or returned upwards (as return values). This allows the function to refer to variables of its environment, even if the surrounding code is no longer active.

如果您有 a),则该函数可以引用其环境的变量某些语言,那么问题出现了 b)?有些语言包含 a),但不包含 b)。在函数编程世界中, a)(函数)和 b (作为闭包)现在是常态。 Smalltalk已有 a)阻止是匿名函数)很长一段时间,但后来有些方言Smalltalk添加了 b)闭包)。

If you have a) in some language, then the question comes up what to do about b)? There are languages that have a), but not b). In the functional programming world a) (functions) and b (functions as closures) is nowadays the norm. Smalltalk had a) (blocks are anonymous functions) for a long time, but then some dialects of Smalltalk added support for b) (blocks as closures).

你可以想象,如果你给语言添加函数和闭包,你会得到一个稍微不同的编程模型。

You can imagine that you get a slightly different programming model, if you add functions and closures to the language.

从实用的角度来看,匿名函数添加了一些简短的符号,你可以传递或调用函数。这可能是一件好事。

From a pragmatic view, the anonymous function adds some short notation, where you pass or invoke functions. That can be a good thing.

闭包(函数加上绑定)允许你例如创建一个可以访问一些变量的函数(例如计数器值)。现在你可以将该函数存储在对象中,访问它并调用它。函数对象的上下文现在不仅是它可以访问的对象,而且还是它通过绑定访问的变量。这也是有用的,但你可以看到变量绑定与访问对象变量现在是一个问题:当应该是一个词法变量(可以在闭包中访问)和什么时候是某个对象的变量( slot )。什么时候应该是闭包或对象?您可以以类似的方式使用两者。对学生学习的一个通常的编程习惯Scheme(一个Lisp方言)是使用闭包写一个简单的对象系统。

The closure (function plus binding) allows you for example to create a function that has access to some variables (for example to a counter value). Now you can store that function in an object, access it and invoke it. The context for the function object is now not only the objects it has access to, but also the variables it has access to via bindings. This is also useful, but you can see that variable bindings vs. access to object variables now is an issue: when should be something a lexical variable (that can be accessed in a closure) and when should it be a variable of some object (a slot). When should something be a closure or an object? You can use both in the similar ways. A usual programming exercise for students learning Scheme (a Lisp dialect) is to write a simple object system using closures.

结果是一个更复杂的编程语言和更复杂运行时模型。太复杂了?

The result is a more complicated programming language and a more complicated runtime model. Too complicated?

这篇关于Java需要关闭吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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