When I run the following code I get the expected output:
var a = [1, 2, 3]
var b = a
if a === b { println("a === b") }
if a == b { println("a == b") }
Output:
a === b
a == b
However, if I import Foundation suddenly I only a new output:
import Foundation
var a = [1, 2, 3]
var b = a
if a === b { println("a === b") }
if a == b { println("a == b") }
Output:
a == b
Does anyone know what is going on behind the scenes? Thank you.
It has to do with mutability, though the import Foundation
part is interesting. My guess is that it has to do with NSArray
or NSObject
copying.
You can test it by changing your var
to let
and watching ===
become true again.
Test the different permutations and see what happens:
var a = [1, 2, 3]
var b = a
if a === b { println("arrays are the same instance") } // false
if a == b { println("arrays are equivalent") } // true
var a = [1, 2, 3]
let b = a
if a === b { println("arrays are the same instance") } // false
if a == b { println("arrays are equivalent") } // true
let a = [1, 2, 3]
var b = a
if a === b { println("arrays are the same instance") } // false
if a == b { println("arrays are equivalent") } // true
let a = [1, 2, 3]
let b = a
if a === b { println("arrays are the same instance") } // true
if a == b { println("arrays are equivalent") } // true
This is actually the proper behavior for making a defensive copy, only when necessary.
If your source is mutable, you have to make a copy otherwise changes could be made externally. If you want a mutable copy you need to make a copy of course.
The only time a copy is not made is when both the source and copy are immutable, because that would be wasteful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With