0

I've declared my struct to conform to Equatable

struct LocationInfo: Equatable {

    // MARK: - Properties

    let gatewayId: GatewayId?
    let gatewaySerialNumber: String?
    let locationId: LocationId
    let locationName: String
    let role: UserRoleModel
    var rooms: [RoomInfo]
    let gateways: [GatewayInfo]
    var devices: [any DeviceInfoProtocol]
    let devicesGroups: [DeviceGroupsInfo]
    let membershipId: MembershipId?
    let partners: [PartnerConnection]
    let scripts: [AutomationScript]

all the types used are either String typealiases or Equatable or Hashable entities. All of them (I've checked and doublechecked). Why do I get

Type 'LocationInfo' does not conform to protocol 'Equatable'

and am therefore forced to implement == ?

Anton Tropashko
  • 5,486
  • 5
  • 41
  • 66
  • 4
    The problem is the `any`. Just because two types conform to the same protocol and both conform to Equatable doesn’t mean they can be compared to each other. – Joakim Danielson Aug 02 '23 at 11:03
  • You may triple check, if actually _all_ types conform to `Equatable`. Based on own experience ;) – CouchDeveloper Aug 02 '23 at 11:03
  • Joakim, would you explain as to a 6 year old: any reason something like https://stackoverflow.com/questions/34778950/how-to-compare-any-value-types does not come out of the box for any? – Anton Tropashko Aug 02 '23 at 11:18
  • Check the subtypes for conformance, if you’ve made custom keys check for typos but “any” can require conformance but doesn’t actually conform. – lorem ipsum Aug 02 '23 at 11:28
  • 1
    The compiler only synthesises the equality function by conforming to `Equatable` if the operands have the _identical_ type. The type `[any SomeProtocol]` does not conform to `Equatable`. It does not, because `any SomeProtocol` does not conform to `Equatable`. `any SomeProtocol` cannot because it's an "existential" and its wrapped value does not have the requirement to be of the same or a specific type. – CouchDeveloper Aug 02 '23 at 11:30
  • 2
    Do you really need the *lazy* way that the compiler does all work for you or could two instances be equal for example if `LocationId` and `name` are equal? The *default implementation* is rather convenient but implementing `==` yourself and comparing one ore two properties can be much more performant (this applies also to `Hashable`). – vadian Aug 02 '23 at 11:56
  • 1
    If you maje `struct LocationInfo2: Equatable {}` and put one per one (or n per n) to find the culprit maybe? – Larme Aug 02 '23 at 12:24
  • Great. Thanks a lot! – Anton Tropashko Aug 02 '23 at 13:04
  • Speaking of lazy. I had to reflect upon that for a bit and I have to admit I am. I would have used Python if I had a chance on iOS, writing tons of boilerplate is tedious and we have sadly no Jr's on the team to outsource that work to. So given no slaves the help of the language (or lack of such) goes a long way towards developer retention throughout their careers. Here ya go. And I still have 178 characters to expend. – Anton Tropashko Aug 03 '23 at 16:28

0 Answers0