Skip to content

Instantly share code, notes, and snippets.

@endavid
Created February 26, 2020 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save endavid/328e1613e8b098dfa6a5e9e00fb4b398 to your computer and use it in GitHub Desktop.
Save endavid/328e1613e8b098dfa6a5e9e00fb4b398 to your computer and use it in GitHub Desktop.
#swift triple equality examples for #medium
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