Using inout keyword: is the parameter passed-by-reference or by copy-in copy-out (/call by value result)

前端 未结 1 1603
半阙折子戏
半阙折子戏 2021-01-18 22:51

Question: Based on the information and discussion below: Are inout parameters passed-by-reference or by copy-in copy-out?

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-18 23:07

    The next two paragraphs in the Language Reference describes it more in detail:

    In-Out Parameters

    This behavior is known as copy-in copy-out or call by value result. For example, when a computed property or a property with observers is passed as an in-out parameter, its getter is called as part of the function call and its setter is called as part of the function return.

    As an optimization, when the argument is a value stored at a physical address in memory, the same memory location is used both inside and outside the function body. The optimized behavior is known as call by reference; it satisfies all of the requirements of the copy-in copy-out model while removing the overhead of copying. Do not depend on the behavioral differences between copy-in copy-out and call by reference.

    So it's de facto "pass by reference"

    0 讨论(0)
提交回复
热议问题