Equatable Interface
struct interface Equatable {
pub fun equals(_ other: {Equatable}): Bool
}// Declare a struct named `Cat`, which has one field named `id`
// that has type `Int`, i.e., the identifier of the cat.
//
// `Cat` also will implement the interface `Equatable`
// to allow cats to be compared for equality.
//
struct Cat: Equatable {
pub let id: Int
init(id: Int) {
self.id = id
}
pub fun equals(_ other: {Equatable}): Bool {
if let otherCat = other as? Cat {
// Cats are equal if their identifier matches.
//
return otherCat.id == self.id
} else {
return false
}
}
}
Cat(1) == Cat(2) // is `false`
Cat(3) == Cat(3) // is `true`Last updated