0

I have an enum that has several cases with associated values and it conforms to Equatable.

I now need to add another case that uses a closure for its associated value and was wandering how I could make it conform to Equatable without having to write out the custom Equatable conformance.

For example say I had an enum like below

enum CoordinatorAction: Equatable {
    case closed
    case goToHome(isSignedIn: Bool)
    case addedItem(_ itemID: String)
    case removedItem(_ itemID: String)
}

How could I then add the didSuccessfulyAddItem enum with A closure whilst still having automatic conformance to equatable without getting a compile error?

enum CoordinatorAction: Equatable {
    case closed
    case goToHome(isSignedIn: Bool)
    case addedItem(_ itemID: String)
    case removedItem(_ itemID: String)
    case didSuccessfulyAddItem(_ callback: (Bool) -> Void)
}

Thanks

timbre timbre
  • 12,648
  • 10
  • 46
  • 77
Edward
  • 2,864
  • 2
  • 29
  • 39
  • 2
    You can’t have automatic conformance with this. You would have to write your own == function. – Fogmeister Mar 23 '23 at 16:24
  • You can create a custom property wrapper to ignore properties from `Equatable`/`Hashable` conformance without giving up on the auto-synthesised conformance as [this gist](https://gist.github.com/Iron-Ham/d0eb72822f0965a3688241a70ec08f8c) shows. – Dávid Pásztor Mar 23 '23 at 16:42
  • There is no way to compare if two closures are equal, see for example https://stackoverflow.com/q/45270371/1187415. – Martin R Mar 23 '23 at 18:27
  • Thanks all. I didn't think there was a way and was just hoping I was missing something. Appreciate the answers. – Edward Mar 24 '23 at 09:10

0 Answers0