闭包的确切定义是什么? [英] What is the exact definition of a closure?

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

问题描述

我已经阅读过关于stackflow和其他来源的闭包的一些主题,有一件事仍然让我困惑。从技术上来说,一个闭包只是一组包含函数代码和该函数中绑定变量的值的数据。

I've read through previous topics on closures on stackflow and other sources and one thing is still confusing me. From what I've been able to piece together technically a closure is simply the set of data containing the code of a function and the value of bound variables in that function.

换句话说,技术上下面的C函数应该是一个闭包从我的理解:

In other words technically the following C function should be a closure from my understanding:

int count()
{
    static int x = 0;

    return x++;
}

然而,我读到的一切似乎意味着闭包必须以某种方式传递函数作为第一类对象。此外,通常似乎暗示关闭不是程序性规划的一部分。这是一个解决方案与解决的问题过分相关的情况,或者我误解了确切的定义?

Yet everything I read seems to imply closures must somehow involve passing functions as first class objects. In addition it usually seems to be implied that closures are not part of procedural programming. Is this a case of a solution being overly associated with the problem it solves or am I misunderstanding the exact definition?

推荐答案

我理解一个闭包也必须访问调用上下文中的变量。闭包通常与函数式编程相关。语言可以具有来自不同类型的编程观点,功能,过程,命令式,声明性等的元素。它们从在指定上下文中被关闭的名称。它们也可以具有词法绑定,因为它们可以使用在该上下文中使用的相同名称来引用指定的上下文。您的示例没有引用任何其他上下文,但是全局静态。

From what I understand a closure also has to have access to the variables in the calling context. Closures are usually associated with functional programming. Languages can have elements from different types of programming perspectives, functional, procedural, imperative, declarative, etc. They get their name from being closed over a specified context. They may also have lexical binding in that they can reference the specified context with the same names that are used in that context. Your example has no reference to any other context but a global static one.

维基百科

封闭关闭自由变量(非本地变量的变量)

A closure closes over the free variables (variables which are not local variables)

这篇关于闭包的确切定义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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