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-函数和闭包是引用类型的更多相关文章

  1. 《javascript高级程序设计》第五章 reference types

    第5 章 引用类型5.1 Object 类型5.2 Array 类型 5.2.1 检测数组 5.2.2 转换方法 5.2.3 栈方法 5.2.4 队列方法 5.2.5 重排序方法 5.2.6 操作方法 ...

  2. A Swift Tour(3) - Functions and Closures

    Functions and Closures 使用func来声明函数,通过括号参数列表的方式来调用函数,用 --> 来分割函数的返回类型,参数名和类型,例如: func greet(name: ...

  3. SQL Fundamentals: Using Single-Row Functions to Customize Output使用单行函数自定义输出

    SQL Fundamentals || Oracle SQL语言 DUAL is a public table that you can use to view results from functi ...

  4. iOS: 学习笔记, 值与引用类型(译自: https://developer.apple.com/swift/blog/ Aug 15, 2014 Value and Reference Types)

    值和引用类型 Value and Reference Types 在Swift中,有两种数据类型. 一是"值类型"(value type), 它是每一个实例都保存有各自的数据,通常 ...

  5. swift语言点评十-Value and Reference Types

    结论:value是拷贝,Reference是引用 Value and Reference Types Types in Swift fall into one of two categories: f ...

  6. 初探 C# 8 的 Nullable Reference Types

    溫馨提醒:本文提及的 C# 8 新功能雖已通過提案,但不代表將來 C# 8 正式發布時一定會納入.這表示我這篇筆記有可能白寫了,也表示您不必急著瞭解這項新功能的所有細節,可能只要瞄一下底下的「概要」說 ...

  7. 【译】尝试使用Nullable Reference Types

    随着.NET Core 3.0 Preview 7的发布,C#8.0已被认为是“功能完整”的.这意味着它们的最大亮点Nullable Reference Types,在行为方面也被锁定在.NET Co ...

  8. php的匿名函数和闭包函数

    php的匿名函数和闭包函数 tags: 匿名函数 闭包函数 php闭包函数 php匿名函数 function use 引言:匿名函数和闭包函数都不是特别高深的知识,但是很多刚入门的朋友却总是很困惑,因 ...

  9. scala学习笔记4:函数和闭包

    以下主要记录的是看完scala in programming这本书functions and closures(第八章)后的要点总结. 1,函数可以存在的地方:函数方法,嵌套函数. 2,关于funct ...

随机推荐

  1. npm update常用命令使用

    一.更新 npm-check检查更新 npm install -g npm-check npm-check 2. npm-upgrade更新 npm install -g npm-upgrade np ...

  2. window.open以post方式提交(转)

    function openWindowWithPost(url,name,keys,values) { var newWindow = window.open(url, name); if (!new ...

  3. Redis - 事务操作

    Redis的事务基于四个命令: MULTI EXEC DISCARD WATCH 创建事务 Redis的事务从一个MULTI命令开始,MULTI总会命令返回"ok". 接着就可以开 ...

  4. js实现栈

    栈是一种先进后出的特殊线性表结构,存储上分链式存储和顺序存储两种方式 链式存储: function LinkedStack() { let Node = function (ele) { this.e ...

  5. [javaEE] jsp入门

    Servlet写java代码很好,但是拼接html的时候,非常不方便 JSP可以在html中嵌套java代码,这样在展示的时候,就会比较方便 Tomcat帮我们把jsp的页面翻译成了Servlet去运 ...

  6. 流畅的python和cookbook学习笔记(三)

    1.双向队列 collections.deque 类(双向队列)是一个线程安全.可以快速从两端添加或者删除元素的数据类型. rotate和popleft操作,rorate可以把前后元素换位.pople ...

  7. 第1章:程序设计和C语言(C语言入门)

    一.程序和程序语言 1,程序的概念:完成某项事物所预设的活动方式. 2,程序设计:人们描述计算机要做的工作. 二 .程序设计语言及其发展 1.机器语言,2汇编语言,3高级语言{a)编译,b)解释}: ...

  8. 关于webstorm启动后闪退

    总是提示内存不足,就把内容该成了2048 在启动时候就闪退,无法进去编辑器 找到安装目录下的bin文件夹打开找到WebStorm.exe.vmoptions文件打开 把下面选项设置为 -Xmx1024 ...

  9. Java—IO流 对象的序列化和反序列化

    序列化的基本操作 1.对象序列化,就是将Object转换成byte序列,反之叫对象的反序列化. 2.序列化流(ObjectOutputStream),writeObject 方法用于将对象写入输出流中 ...

  10. css tips: 清除float影响,containing的div跟随floated sub等

    /** * For modern browsers * 1. The space content is one way to avoid an Opera bug when the * content ...