by i_s · 10y ago · 0 comments
That is at best misleading, as it is not true for collections. From the Swift docs:
> If you create an array, a set, or a dictionary, and assign it to a variable, the collection that is created will be mutable. This means that you can change (or mutate) the collection after it is created by adding, removing, or changing items in the collection. If you assign an array, a set, or a dictionary to a constant, that collection is immutable, and its size and contents cannot be changed.
Swift is the only language that I've seen that actually does make a distinction depending on the keyword used.
And it has to, to make 'let' create truly immutable objects. Without them, this:
var a = [1,2,3]
let b = a
a += [4,5,6]
would change b.class Foo { var x = 0 }
let a = [Foo(), Foo(), Foo()]
var b = a
a[0].x = 1
var c = b // [0].x == 1 ;(
let d = a // [0].x == 1 ;(