Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

According to the Swift language guide Strings, Arrays, and Dictionaries are all copied on assignment to a variable. So wouldn't the author's primary assumption (that let a and var b refer to the same array) be false? And this whole issue is null?

https://developer.apple.com/library/prerelease/mac/documenta...

Assignment and Copy Behavior for Strings, Arrays, and Dictionaries

Swift’s String, Array, and Dictionary types are implemented as structures. This means that strings, arrays, and dictionaries are copied when they are assigned to a new constant or variable, or when they are passed to a function or method.

This behavior is different from NSString, NSArray, and NSDictionary in Foundation, which are implemented as classes, not structures. NSString, NSArray, and NSDictionary instances are always assigned and passed around as a reference to an existing instance, rather than as a copy.

NOTE The description above refers to the “copying” of strings, arrays, and dictionaries. The behavior you see in your code will always be as if a copy took place. However, Swift only performs an actual copy behind the scenes when it is absolutely necessary to do so. Swift manages all value copying to ensure optimal performance, and you should not avoid assignment to try to preempt this optimization.



> According to the Swift language guide Strings, Arrays, and Dictionaries are all copied on assignment to a variable.

This might've been a recent change in docs. Quoting part of the same section from my copy of the Swift book downloaded a while ago (emphasis mine):

‘The assignment and copy behavior for Swift’s Array type is more complex than for its Dictionary type…

‘If you assign an Array instance to a constant or variable, or pass an Array instance as an argument to a function or method call, the contents of the array are not copied at the point that the assignment or call takes place. Instead, both arrays share the same sequence of element values. When you modify an element value through one array, the result is observable through the other.

‘For arrays, copying only takes place when you perform an action that has the potential to modify the length of the array.’

As an aside, it would be nice if Apple offered some kind of change log or a “what's changed” page for Swift docs (though to my knowledge they had made no promises regarding spec stability yet).


Revision history for the main Swift book:

https://developer.apple.com/library/prerelease/ios/documenta...

Note that you have to delete the book in iBooks and download it again to get the new version.


Agree. Note that the quoted text is from the docs that were released with beta3 and is a big (and welcome) change from the original docs.


No. As stated in the article, the array was not copied when it was assigned to var b as changes made to it via b also showed up in a.


Which was a bug in beta 2 that was corrected in beta 3. So the behavior in beta 3 matches the design quoted above. Hence kipple is correct.


It was a design bug not a implementation bug.

Beta 2 behaviour matched the docs at that time but there was plenty of pushback about the behaviour in radars, Apple dev forums[0] and on the wider web[1].

Chris Lattner announced[0] the planned change on the forum 19th June.

[0] https://devforums.apple.com/message/989944#989944

[1] http://blog.human-friendly.com/swift-arrays-too-swift-and-fl...


Thank you for the clarification.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: