HomeiOS Developmentios - How can I conform a Swift enum to `Equatable` when...

ios – How can I conform a Swift enum to `Equatable` when it has an any existential as considered one of its related values?


Suppose I’ve:

protocol MyError: Error, Equatable {
  var errorDispalyTitle: String { get }
  var errorDisplayMessage: String { get }
}

enum ContentState {
  case .loading
  case .error(any MyError)
  case .contentLoaded
}

If i had been to implement Equatable in ContentState so I can evaluate throughout unit exams I find yourself with an issue as a result of I’ve to check two any MyError sorts that are boxed and may be of two totally different underlying sorts.

extension ContentState: Equatable {
  static func == (lhs: ContentState, rhs: ContentState) -> Bool {
    swap (lhs, rhs) {
    case (.loading, .loading):
      return true
    case (.contentLoaded, .contentLoaded):
      return true
    case (.error(let lhsError), .error(let rhsError)):
      // TODO: have to check if each underlying sorts are match after which if they're equal in worth
    default:
      return false
    }
  }
}

How do I do that?

I attempted lifting the generic from the existential kind there to the kind (e.g. ContentState<Error: MyError>) which lets it compile when implementing equatable because it is aware of how one can infer the kind, however the issue is for whichever class makes use of that enum it doesnt matter which kind is receiving of it, solely that’s any kind of it, and if I do not implement the any existential it begins requiring the generic to be propagated up the chain.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments