Home > OS >  Is there an error prone way to exclude certain properties from struct during equality comparison wit
Is there an error prone way to exclude certain properties from struct during equality comparison wit

Time:10-26

We like the auto equality behavior of struct, by just simply conform Equatable protocol.

Sometimes, we would like to exclude some properties out from struct equality comparison.

Currently, this is the method we are using.

struct House: Equatable {
    var field_0: String
    var field_1: String
    ...
    var field_50: String

    static func == (lhs: House, rhs: House) -> Bool {
        if lhs.field_0 != rhs.field_0 { return false }
        if lhs.field_1 != rhs.field_1 { return false }
        ...
        if lhs.field_48 != rhs.field_48 { return false }
        // Ommit field_49
        if lhs.field_50 != rhs.field_50 { return false }

        return true
    }
}

But, such methodology is pretty error prone, especially when we add in new field in the struct.

Is there a way to exclude certain properties fields from equality comparison, without writing our own == function?

CodePudding user response:

Is there a way to exclude certain properties fields from equality comparison, without writing our own == function?

No. At present, the only alternative to automatic synthesis of Equatable conformance is good old fashioned writing code.

  • Related