确切地说,“关闭”与“关闭”之间有什么区别?和一个“块”? [英] Exactly what is the difference between a "closure" and a "block"?

查看:212
本文介绍了确切地说,“关闭”与“关闭”之间有什么区别?和一个“块”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现很多人可以交替使用 closure block 一词。这些人中的大多数人无法解释他们在说什么。

I've found that lots of people use the words closure and block interchangeably. Most of these people can't explain what they're talking about.

一些Java程序员(甚至是来自非常昂贵的咨询公司的程序员)都将匿名内部类称为块。和关闭-但我知道这不是真的。 (您不能在定义变量的方法范围内传递可变变量...)

Some Java programmers (even ones from really expensive consultancies) talk about anonymous inner classes as "blocks" and "closures" - but I know this isn't true. (You can't pass mutable variables in from the scope of the method in which they're defined...)

我在寻找:


  • 精确的计算机科学块的定义

  • 精确的计算机科学定义结束

  • 澄清两者之间的区别

  • a precise, computer science definition of a block
  • a precise, computer science definition of a closure
  • and clarification on the difference between the two.

我真的很想查看链接,文章或书籍关于这些内容的引用

I'd really like to see links, articles or book references on these please.

推荐答案

虽然 block 只是一段可以由语句和声明组成的代码,但是 closure 是真正的一类对象,一个真正的变量,其值具有一个块。

While a block is just a piece of code that can be composed by statements and declarations but nothing else, a closure is a real first-class object, a real variable that has a block as its value.

主要区别在于,块只是将指令分组在一起(例如, while 语句),而闭包是一个变量,其中包含一些可以被ex

The main difference is that a block simply groups instructions together (for example the body of a while statement), while a closure is a variable that contains some code that can be executed.

如果您通常有一个闭包,则可以将其作为函数的参数传递,对其进行归一化和去归一化,并且主要调用它!

If you have a closure usually you can pass it as a parameter to functions, currify and decurrify it, and mainly call it!

Closure c = { println 'Hello!' }
/* now you have an object that contains code */
c.call()

当然,闭包更强大,它们是变量,可用于定义自定义行为对象(通常在编程时必须使用接口或其他OOP方法)。

Of course closures are more powerful, they are variables and can be used to define custom behaviour of objects (while usually you had to use interfaces or other OOP approaches in programming).

您可以想到闭包

You can think of a closure as a function that contains what that function does inside itself.

块是有用的,因为它们允许对变量进行范围界定。通常,当您在范围内定义变量时,您可以覆盖外部定义而不会出现任何问题,并且新的定义将仅在执行块期间存在。

Blocks are useful because they allow scoping of variables. Usually when you define a variable inside a scope you can override the outer definitions without any problems and new definitions will exist just during the execution of block.

for (int i = 0; i < 10; ++i)
{
     int t = i*2;
     printf("%d\r\n", t);
}

t 已定义在块内( for 语句的主体),并将持续到该块内。

t is defined inside the block (the body of the for statement) and will last just inside that block.

这篇关于确切地说,“关闭”与“关闭”之间有什么区别?和一个“块”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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