Created
February 26, 2020 17:58
-
-
Save endavid/328e1613e8b098dfa6a5e9e00fb4b398 to your computer and use it in GitHub Desktop.
#swift triple equality examples for #medium
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Bread { | |
let rating : Int | |
} | |
class Cheese { | |
let rating : Int | |
init(rating: Int) { | |
self.rating = rating | |
} | |
} | |
var c1 = Cheese(rating: 0) | |
var c2 = Cheese(rating: 0) | |
var c3 = c1 // same reference | |
var c4 = Cheese(rating: 1) | |
c1 == c2 // error, == not defined | |
c1 === c2 // false | |
c3 === c1 // true | |
var b1 = Bread(rating: 0) | |
var b2 = Bread(rating: 0) | |
b1 == b2 // error, == not defined | |
b1 === b2 // error, they aren't references! | |
let cheeses = [c1, c2] | |
cheeses.contains(c1) // error, Cheese not Equatable | |
cheeses.contains { $0 === c1 } // true | |
cheeses.contains { $0 === c3 } // true | |
cheeses.contains { $0 === c4 } // false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment