0
votes

I'm just playing around swift with playground.

This code compiled on beta 2 and previous, but not on beta 3 or beta 4:

var myArray = ["foo1", "foo2", "foo3"]

var myArray2 = myArray
myArray2 === myArray

myArray2 += "hello"
myArray2 === myArray

It states that Type [String] does not conform to protocol AnyObject.

On Swift blog we read that in b3:

Array has been completely redesigned to have full value semantics to match the behavior of Dictionary and String. Now a let array is completely immutable, and a var array is completely mutable.

So is it correct to say that we lost reference equality operator === for arrays?

Operator == is obviously not the same thing, since it compares the array elements.

1
=== is only for class (reference-type) and Array is struct (value-type) - Bryan Chen
In beta 2 and previous that's simply not true. Arrays was a mixture, being copied only when modified. - Teejay

1 Answers

0
votes

Yes it is fair to say that we lost === for arrays. Arrays were made true value types which means they are always copied when assigned to a new variable. That makes === pointless because it would never return true.

Reference types (or classes) are the only place where === makes sense since they are the only types that can have two variables reference the same value. That is why the error says that the array does not conform to the protocol AnyObject. AnyObject is a protocol for all Classes.