Swift Arrays are Fixed!

If you've been following along with Swift, you would have noticed some very interesting semantics with arrays: they were completely broken! Interestingly enough, there were many people arguing in the forums and other places that this was "ok" and "good for performance".

Fortunately for us, the Swift team decided on sanity. So instead of code like this working (Xcode 6, Beta 2):

let array = [5, 1, 0, 10]
array[1] = 0   // just changed a value in the "const" array, ok…
array += 5     // compile time error: good

var sortedArray = sort(array)   // original array updated, oops!
sortedArray.append(4)

sortedArray                     // prints: [0, 0, 5, 10, 4]
array                           // prints: [0, 0, 5, 10]

We can return to the world of sanity in Xcode 6, Beta 3:

let array = [5, 1, 0, 10]
array[1] = 0      // compile time error: good
array += 5        // compile time error: good

var sortedArray = [Int](array)
sortedArray.sort(<)
sortedArray.append(4)

sortedArray                // prints: [0, 1, 5, 10, 4]
array                      // prints: [5, 1, 0, 10]

Many late-nights of debugging arrays have just been saved. Now, all we need, is a way to express const on class types. =)

Swift Arrays are Fixed!