Skip to content

Instantly share code, notes, and snippets.

@CassiusPacheco
Created February 23, 2021 23:00
Show Gist options
  • Save CassiusPacheco/4378d30d69316e4a6ba28a0c3af72628 to your computer and use it in GitHub Desktop.
Save CassiusPacheco/4378d30d69316e4a6ba28a0c3af72628 to your computer and use it in GitHub Desktop.
This abstraction contains either a success data of generic type `T` or an error of type `ErrorCode` as its result.
enum ErrorCode: Equatable, Error {
case unknown
// Top level server error
case serverIssue
// Network
case noNetworkConnection
}
enum DataResult<T> {
case success(value: T)
case error(ErrorCode)
var value: T? {
switch self {
case .success(value: let value):
return value
case .error:
return nil
}
}
var error: ErrorCode? {
switch self {
case .success:
return nil
case let .error(error):
return error
}
}
}
extension DataResult: Equatable where T: Equatable {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment