When implementing the Equality protocol for a struct: is it recommended to always compare all of the structs fields? Or is it ok to ignore some fields of a struct in the comparison?
Eg:
struct ControlPoint : Equatable {
let position: Vec2
let pointId: Int
let ownerId : Int
}
func ==(lhs: ControlPoint, rhs: ControlPoint) -> Bool {
return lhs.pointId == rhs.pointId && lhs.ownerId == rhs.ownerId // ignoring the position
}In my case I do not really care about the position. I have some old ControlPoint and some new ControlPoint and I want to check if they represent the same internal point. But the pointId and ownerId itself are implementation details which I do not want to expose ie it do not want the client of the class to compare both of them manually
Is using the Equality protocol the right thing to do in this situation or is it confusing for the client that two structs with different values are treated the same?
Is it better two create my own protocol like this?:
protocol PointEquality {
public func areSamePoint(lhs: Self, rhs: Self) -> Bool
}
struct ControlPoint : PointEquality {
let position: Vec2
let pointId: Int
let ownerId : Int
}
func areSamePoint(lhs: ControlPoint, rhs: ControlPoint) -> Bool {
return lhs.pointId == rhs.pointId && lhs.ownerId == rhs.ownerId // ignoring the position
}When using classes it seems more obvious that a class can have some internal state which is not considered in the equality check. But with structs this seems weird to me. But I do not like using a class for the ControlPoint either because it has no mutable fields and it is only created, used for rendering and hittesting and thrown away ie has no lifecycle.