I have a situation where I have 2 types, which must be structs for other reasons.
These two types are very similar, but are slightly different. However, their hashing & equality functions are exactly the same.
struct S1: Hashable {
let data: String
let otherData: String
// ... Equality & Hash functions, both only use data
}
struct S2: Hashable {
let data: String
let otherData: Int
// ... Equality & Hash functions, both only use data
}
Suppose I had a Set
like this:
var set: Set<S1> = ...
How could I do a contains
operation on set
, but use a S2
object instead, whilst maintaining the O(1) time complexity?
let obj: S2 = ...
set.contains(obj)
Is there a way to provide a custom equality & hashing object, or have some equivalent behavior?