You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When the function is called, the value of the argument is copied.
In the body of the function, the copy is modified.
When the function returns, the copy’s value is assigned to the original argument.
By reading that, if any changes are made to the argument outside of the function between. step 1 and 3, those changes would be lost when the function returns. The scenario I'm describing would be:
Pass array a `[1, 2, 3]` as an In-Out argument to function `f()`
While `f()` is executing, outside the function, `a` gets modified to add another value.
At this point `a` is `[1, 2, 3, 4]`
When `f()` finishes executing the copy of `a` copied back to the calling context, losing the last value 4.
That's the expected behavior according to the documentation. I have this test case showing it doesn't work that way. Down below the documentation mentions an "optimization" that "satisfies all of the requirements of the copy-in copy-out model while removing the overhead of copying." That last part is misleading. This "optimization" explains the behavior I'm seeing, but if I were to "Write your code using the model given by copy-in copy-out, without depending on the call-by-reference optimization" I would be surprised the system is not working as expected.
Attached is the test case
PS: The double quotes above are to show what's in the documentation verbatim.
The text was updated successfully, but these errors were encountered:
Attachment: Download
Additional Detail from JIRA
md5: 40885081a51d49565b9dd89413fc2e4b
Issue Description:
The documentation here https://docs.swift.org/swift-book/ReferenceManual/Declarations.html#ID545 states:
In-out parameters are passed as follows:
When the function is called, the value of the argument is copied.
In the body of the function, the copy is modified.
When the function returns, the copy’s value is assigned to the original argument.
By reading that, if any changes are made to the argument outside of the function between. step 1 and 3, those changes would be lost when the function returns. The scenario I'm describing would be:
Pass array a `[1, 2, 3]` as an In-Out argument to function `f()`
While `f()` is executing, outside the function, `a` gets modified to add another value.
At this point `a` is `[1, 2, 3, 4]`
When `f()` finishes executing the copy of `a` copied back to the calling context, losing the last value 4.
That's the expected behavior according to the documentation. I have this test case showing it doesn't work that way. Down below the documentation mentions an "optimization" that "satisfies all of the requirements of the copy-in copy-out model while removing the overhead of copying." That last part is misleading. This "optimization" explains the behavior I'm seeing, but if I were to "Write your code using the model given by copy-in copy-out, without depending on the call-by-reference optimization" I would be surprised the system is not working as expected.
Attached is the test case
PS: The double quotes above are to show what's in the documentation verbatim.
The text was updated successfully, but these errors were encountered: