加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

关闭的Groovy概念

发布时间:2020-12-14 16:33:50 所属栏目:大数据 来源:网络整理
导读:我对Groovy documentation中使用的术语“闭包”感到有点困惑.根据文档,他们对闭包的定义看起来更像是匿名函数或lambda. 我理解支持第一类函数的语言通常允许形成闭包.但是,这两个概念有所区别. 例如,根据Groovy文档: A closure in Groovy is an anonymous c
我对Groovy documentation中使用的术语“闭包”感到有点困惑.根据文档,他们对闭包的定义看起来更像是匿名函数或lambda.

我理解支持第一类函数的语言通常允许形成闭包.但是,这两个概念有所区别.

例如,根据Groovy文档:

A closure in Groovy is an anonymous
chunk of code that may take arguments,
return a value,and reference and use
variables declared in its surrounding
scope.

In functional language parlance,such an anonymous code block might be referred to as an anonymous lambda expression in general or lambda expression with unbound variables or a closed lambda expression if it didn’t contain references to unbound variables (like threshold in the earlier example). Groovy makes no such distinction.

然后根据关闭时的Wikipedia页面:

The term closure is often mistakenly used to mean anonymous function.

我错过了什么?

解决方法

正如它在 page you linked to in the groovy docs的下一段中所述:

Strictly speaking,a closure can’t be defined. You can define a block of code that refers to local variables or fields/properties,but it becomes a closure only when you “bind” (give it a meaning) this block of code to variables. The closure is a semantic concept,like an instance,which you cannot define,just create. Strictly spoken a closure is only a closure if all free variables are bound. Unless this happens it is only partially closed,hence not really a closure. Since Groovy doesn’t provide a way to define a closed lambda function and a block of code might not be a closed lambda function at all (because it has free variables),we refer to both as closure – even as syntactic concept. We are talking about it as syntactic concept,because the code of defining and creating an instance is one,there is no difference. We very well know that this terminology is more or less wrong,but it simplifies many things when talking about code in a language that doesn’t “know” the difference.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读