函数和闭包的区别是什么?

时间:2023-01-15 17:43:03

I have been reading the swift programming guide in iBooks. Could someone explain to me what is the difference between a function and a closure. Is it just that it has no name and can be used in expressions?

我一直在读iBooks的swift编程指南。有人能向我解释一下函数和闭包的区别吗?只是它没有名字,可以用来表达吗?

2 个解决方案

#1


8  

Functions are, in fact, just named closures. The following are at least conceptually equivalent:

实际上,函数只是命名闭包。以下至少在概念上是等价的:

let foo = { println("hello") }

func foo()->(){ println("hello") }

This gets a little more complicated in the case of using func to declare methods, as there's some interesting bits of sugar added regarding the automatic insertion of public named parameters, etc. func myMethod(foo:Int, bar:Int, baz:Int) becomes func myMethod(foo:Int, #bar:Int, #baz:Int), for example.

在使用func声明方法的情况下,这就有点复杂了,因为在自动插入公共命名参数等方面增加了一些有趣的内容,例如func myMethod(foo:Int, bar:Int, baz:Int)变成了func myMethod(foo:Int, #bar:Int, #baz:Int, #baz:Int)

But it's still true that even methods are just a specific case of closures, and if it's true of closures, it's true of functions and methods as well.

但是,即使是方法也是闭包的特定情况,如果闭包是这样,那么函数和方法也是这样。

#2


7  

First, let's start with definition of Closure, as found in Wikipedia:

首先,让我们从关闭的定义开始,就像在*中发现的那样:

In programming languages, a closure (also lexical closure or function closure) is a function or reference to a function together with a referencing environment—a table storing a reference to each of the non-local variables (also called free variables or upvalues) of that function.

在编程语言中,闭包(也称为词法闭包或函数闭包)是与引用环境一起的函数或引用——一个表,存储对该函数的每个非本地变量(也称为*变量或向上值)的引用。

Closure is the term that is used to refer to a function along with the variables from its environment that it "closes".

闭包是一个术语,用来指一个函数以及它所“关闭”的环境中的变量。

The definition of Closure in Swift is inline with lambdas and blocks in other languages like C# and Ruby.

Swift中闭包的定义与lambdas一致,并在其他语言(如c#和Ruby)中使用block。

As for the difference from functions, from the Swift documentation:

关于与职能的区别,与Swift文件:

Global and nested functions, as introduced in Functions, are actually special cases of closures

在函数中引入的全局函数和嵌套函数实际上是闭包的特殊情况

So all functions are essentially closures that store references to variables in their context.

所以所有的函数本质上都是闭包,在它们的上下文中存储对变量的引用。

Closure expressions are convenient way of writing closures, and provides more terse syntax.

闭包表达式是编写闭包的方便方法,并且提供了更简洁的语法。

#1


8  

Functions are, in fact, just named closures. The following are at least conceptually equivalent:

实际上,函数只是命名闭包。以下至少在概念上是等价的:

let foo = { println("hello") }

func foo()->(){ println("hello") }

This gets a little more complicated in the case of using func to declare methods, as there's some interesting bits of sugar added regarding the automatic insertion of public named parameters, etc. func myMethod(foo:Int, bar:Int, baz:Int) becomes func myMethod(foo:Int, #bar:Int, #baz:Int), for example.

在使用func声明方法的情况下,这就有点复杂了,因为在自动插入公共命名参数等方面增加了一些有趣的内容,例如func myMethod(foo:Int, bar:Int, baz:Int)变成了func myMethod(foo:Int, #bar:Int, #baz:Int, #baz:Int)

But it's still true that even methods are just a specific case of closures, and if it's true of closures, it's true of functions and methods as well.

但是,即使是方法也是闭包的特定情况,如果闭包是这样,那么函数和方法也是这样。

#2


7  

First, let's start with definition of Closure, as found in Wikipedia:

首先,让我们从关闭的定义开始,就像在*中发现的那样:

In programming languages, a closure (also lexical closure or function closure) is a function or reference to a function together with a referencing environment—a table storing a reference to each of the non-local variables (also called free variables or upvalues) of that function.

在编程语言中,闭包(也称为词法闭包或函数闭包)是与引用环境一起的函数或引用——一个表,存储对该函数的每个非本地变量(也称为*变量或向上值)的引用。

Closure is the term that is used to refer to a function along with the variables from its environment that it "closes".

闭包是一个术语,用来指一个函数以及它所“关闭”的环境中的变量。

The definition of Closure in Swift is inline with lambdas and blocks in other languages like C# and Ruby.

Swift中闭包的定义与lambdas一致,并在其他语言(如c#和Ruby)中使用block。

As for the difference from functions, from the Swift documentation:

关于与职能的区别,与Swift文件:

Global and nested functions, as introduced in Functions, are actually special cases of closures

在函数中引入的全局函数和嵌套函数实际上是闭包的特殊情况

So all functions are essentially closures that store references to variables in their context.

所以所有的函数本质上都是闭包,在它们的上下文中存储对变量的引用。

Closure expressions are convenient way of writing closures, and provides more terse syntax.

闭包表达式是编写闭包的方便方法,并且提供了更简洁的语法。