functions and closures are reference types-函数和闭包是引用类型

Posted feng9exe

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了functions and closures are reference types-函数和闭包是引用类型相关的知识,希望对你有一定的参考价值。

Closures Are Reference Types

In the example above, incrementBySeven and incrementByTen are constants, but the closures these constants refer to are still able to increment the runningTotal variables that they have captured. This is because functions and closures are reference types.

Whenever you assign a function or a closure to a constant or a variable, you are actually setting that constant or variable to be a reference to the function or closure. In the example above, it is the choice of closure that incrementByTen refers to that is constant, and not the contents of the closure itself.

This also means that if you assign a closure to two different constants or variables, both of those constants or variables refer to the same closure.

  1. let alsoIncrementByTen = incrementByTen
  2. alsoIncrementByTen()
  3. // returns a value of 50
  4. incrementByTen()
  5. // returns a value of 60

The example above shows that calling alsoIncrementByTen is the same as calling incrementByTen. Because both of them refer to the same closure, they both increment and return the same running total.

 

https://docs.swift.org/swift-book/LanguageGuide/Closures.html

以上是关于functions and closures are reference types-函数和闭包是引用类型的主要内容,如果未能解决你的问题,请参考以下文章

PHP匿名函数闭包function use

Closure call with mismatched arguments: function ‘routes.<anonymous closure>‘

Closure call with mismatched arguments: function ‘routes.<anonymous closure>‘

Closure as the function parameter

Closure as the function parameter

闭包(Closure)和匿名函数(Anonymous function)/lambda表达式的区别