I want to write a partial compare function, defined for all Equatable structs, that would be able to compare two struct instances, but ignoring any properties specified using an argument. Example, where the unicorn operator (🦄 ) represents what I am missing:
struct Struct1: Equatable {
x: Int
y: Int
z: Int
}
func compare<T: Equatable>(_ a: T, _ b: T, ignoring ignoredProperties: [Property]) { // N.B. “Property” is a made-up type.
var alteredA = a
for property in ignoredProperties {
alteredA🦄 property = b🦄 property
}
return alteredA == b
}
let s1 = Struct1(x:1, y:2, z:3)
let s2 = Struct1(x:1, y:2, z:4) // s2 is the same as s1, except for the value of property “z”
compare(s1, s2, ignoring: []) // -> false
compare(s1, s2, ignoring: [Property(Struct1.z)]) // -> true
What can I use instead of 🦄 and Property ?
N.B. I don’t need the ignored properties to be specified at runtime – in this example, they could actually be known at compile time.
N.B. 2. If I need to change my struct into a class, so be it, but I don’t really known how to do what I need using a class either.