Hacker News new | ask | show | jobs
by josephlord 4399 days ago
> Except that you can still make changes that don’t affect length (immutArray[3] = "Whoopsie")

That sounds like a bug to me. I would expect the compiler to detect it as an error (it doesn't). Can anyone point me to the relevant bit of the spec that says you can do that?

1 comments

"Immutability has a slightly different meaning for arrays, however. You are still not allowed to perform any action that has the potential to change the size of an immutable array, but you are allowed to set a new value for an existing index in the array. This enables Swift’s Array type to provide optimal performance for array operations when the size of an array is fixed."

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

So immutability is a code safety feature, except in the case of arrays, where it's a performance optimization. That seems counterintuitive.
Thanks. I think it sounds like a bad decision, it can be compile time checked so won't cause performance issues. I wonder what the cost is in the mutable case for the fact that the length can vary.