Swift

Originator:chris
Number:rdar://17150380 Date Originated:04-Jun-2014
Status:Closed: Duplicate of 17192555 Resolved:10-Jun-2014
Product:Developer Tools Product Version:Xcode 6A215l, swift-600.0.34.4.5
Classification: Reproducible:Always
 
Summary:
Arrays created with "let" should not allow their elements to change. Doing so breaks the principle of least surprise and is likely to cause bugs in concurrent code.

Steps to Reproduce:
1. Open an Xcode playground.
2. Type the following:

        let array = ["a"]
        array[0] = "b"

Expected Results:
A "Cannot assign to the result of this expression" error.

Actual Results:
The array is mutated.

Version:
Xcode 6A215l, swift-600.0.34.4.5

Notes:
I understand that Swift can perform optimizations on semi-mutable arrays that it can't perform on fully mutable ones.

But that doesn't mean you should overload "let"—which for all other struct types means "this is immutable"—to indicate an array is semi-mutable. When I pass an immutable array to another function—or, God forbid, to a block on another queue—I need to be able to trust that the array won't change behind my back.

And importantly, rdar://17136760 is wrong: the Swift book says you *can't* call `unshare()` on constant arrays. That leaves *no way* to use constant arrays safely in Swift.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!