zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Python学习第151天(Django之多对多)
    Python学习第150天(目前正在做的内容介绍)
    挑战日语学习100天:Day11
    挑战日语学习100天:Day10
    hdu3853 LOOPS 期望dp
    最长公共子串
    基于后缀数组的字符串匹配
    高度数组模板
    Jenkins持续集成自动化测试
    自动化上传文件
  • 原文地址:https://www.cnblogs.com/feng9exe/p/10197637.html
Copyright © 2011-2022 走看看