什么是全动力闭合装置?
前几天我参加了 Scala 的 Java 会议,演讲者提到了“全功能闭包”。我很难确定一个对我来说有意义的定义。我已阅读关于 closures 的维基页面,但它确实没有回答我。有人可以帮我给出一个明确的定义吗?甚至可能包括一个简单的例子。
谢谢!
I was at a Java conference on Scala the other day and the speaker referred to 'full powered closures'. I am having a hard time nailing down a definition that makes sense to me. I have read the wiki page on closures but it really didn't answer it for me. Can someone help me with a clear cut definition? Maybe even include a simple example.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
除了维基百科文章中的这句话之外,您不需要更多内容。 Java 缺少的(除了丑陋的语法之外)是从闭包外部绑定自由变量的功能。一个例子:
这里Java编译器会抱怨变量x。
同样的事情在 scala 中工作得很好,而且更简洁:
You don't need more than this sentence from the wikipedia article. What Java lacks (in addition to the ugly syntax) is the feature of binding free variables from outside the closure. An example:
Here the Java compiler will complain about the variable x.
The same thing in scala works just fine and is a lot less verbose:
因为我感觉自己像个初学者(与 DPP 和 Amber 相比),所以我可能会用初学者语言向初学者解释它:
首先,匿名函数(或代码块/lambda 表达式)只是一个没有名称的函数。它可以与这样的变量绑定。
您会看到,该函数没有名称 foo,它可以从 bar 调用。
其次,闭包是一个匿名函数,它有一个未在函数内部定义的变量(变量/值必须在定义函数之前声明)。术语“全动力闭合”可能指的是这种功能。
第一次看到这个,你可能想知道它有什么用处。简而言之,它有很多应用领域:-)
Since I feel like a beginner (compared to DPP and Amber) I might explain it to a beginner in a beginners language:
First, anonymous function (or code block/lambda expression) is simply a function that does not have a name. It could be tied to a variable like this.
You see, the function doesn't have the name foo, it could be called from bar instead.
Second, a closure is an anonymous function that has a variable that is not defined inside the function (the variable/value must have been declared before the function is defined). The term "full powered closure" might refer to this functionality.
First time you see this, you might wonder what it is good for. In short, it has many sectors of application :-)
说话者可能使用“全功率”来表示“类似闭合”的反义词,这就是许多语言实际上都有。
It's possible that the speaker used 'full powered' to mean the opposite of 'closure-like', which is what many languages actually have.
据我所知,全功能闭包并不意味着什么特别的,但它可能意味着:语法的简单性。在 Java 中新建匿名内部类与使用 (a => a < max) 之类的东西之间存在天壤之别。也许能够对所有周围变量(而不仅仅是最终变量)形成闭包。
As far as I know, full powered closures doesn't mean anything in particular but it may mean: simplicity of syntax. There is a world of difference between newing up an anonymous inner class in Java vs. using something like (a => a < max). Perhaps the ability to form closures on all surrounding variables not just final ones.