Skip to content

Instantly share code, notes, and snippets.

@lukevanin
Created March 29, 2018 11:10
Show Gist options
  • Save lukevanin/cfda01cc957b7328a64106095d7b7e90 to your computer and use it in GitHub Desktop.
Save lukevanin/cfda01cc957b7328a64106095d7b7e90 to your computer and use it in GitHub Desktop.
//
// Example showing how to add Equatable protocol conformance to a complex enum using a switch statement.
//
// Useful if you need to compare two enums, e.g:
// let a = ApplicationViewState.content("foo")
// let b = ApplicationViewState.connectionError
// let c = ApplicationViewState.content("foo"
// a == b // false
//
enum ApplicationViewState {
case sync
case content(String)
case login
case connectionError
case versionError(URL)
}
extension ApplicationViewState: Equatable {
static func ==(lhs: ApplicationViewState, rhs: ApplicationViewState) -> Bool {
switch (lhs, rhs) {
case (.sync, .sync):
return true
case (.content(let lhs), .content(let rhs)):
return lhs == rhs
case (.login, .login):
return true
case (.connectionError, .connectionError):
return true
case (.versionError(let lhs), .versionError(let rhs)):
return lhs == rhs
default:
return false
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment